*Memo for string, bytes and bytearray functions:
- My post explains split().
- My post explains splitlines().
- My post explains partition().
- My post explains rpartition().
- My post explains join().
*Memo for a string, bytes and bytearray:
str.rsplit() and bytes.rsplit() or bytearray.rsplit() can split the string and bytes or bytearray respectively from the right to the left as shown below:
*Memo:
- The 1st argument is
sep
(Optional-Default:None
-Type:str
forstr.rsplit()
, Bytes-like object forbytes.rsplit()
andbytearray.rsplit()
or NoneType):- It's the delimiter of the one or more characters and bytes to delimit a string and bytes or bytearray respectively.
- An empty string and bytes or bytearray cannot be set respectively.
- The 2nd argument is
maxsplit
(Optional-Default:-1
-Type:int
):- It decides how many splits are made.
- If it's not set or
-1
, then all possible splits are made.
- If
sep
is set, consecutive delimiters aren't grouped together and are deemed to delimit empty strings and bytes or bytearray respectively:- For example,
'1,,,2'.rsplit(sep=',')
returns['1', '', '', '2']
andb'1,,,2'.rsplit(sep=b',')
returns[b'1', b'', b'', b'2']
.
- For example,
<String>:
v = 'one two three four\nfive\tsix' # ↑ ↑↑ ↑↑↑ print(v) # one two three four # five six print(v.rsplit()) print(v.rsplit(sep=None, maxsplit=-1)) # ['one', 'two', 'three', 'four', 'five', 'six'] print(v.rsplit(maxsplit=0)) # ['one two three four\nfive\tsix'] print(v.rsplit(maxsplit=1)) # ['one two three four\nfive', 'six'] print(v.rsplit(maxsplit=2)) # ['one two three four', 'five', 'six'] print(v.rsplit(maxsplit=3)) # ['one two three', 'four', 'five', 'six'] print(v.rsplit(maxsplit=4)) # ['one two', 'three', 'four', 'five', 'six'] print(v.rsplit(maxsplit=5)) # ['one', 'two', 'three', 'four', 'five', 'six'] # ↓ print(v.rsplit(sep=' ')) # ['one', 'two', '', 'three', '', '', 'four\nfive\tsix'] # ↓↓ print(v.rsplit(sep=' ')) # ['one two', 'three ', 'four\nfive\tsix'] # ↓↓↓ print(v.rsplit(sep=' ')) # ['one two three', 'four\nfive\tsix'] # ↓↓↓↓ print(v.rsplit(sep=' ')) # ['one two three four\nfive\tsix'] print(v.rsplit(sep='')) # ValueError: empty separator
v = 'one-two--three---four-=*!five-=*!?six' # ↑ ↑↑ ↑↑↑ ↑↑↑↑ ↑↑↑↑↑ print(v) # one-two--three---four-=*!five-=*!?six print(v.rsplit()) # ['one-two--three---four-=*!five-=*!?six'] # ↓ print(v.rsplit(sep='-')) # ['one', 'two', '', 'three', '', '', 'four', '=*!five', '=*!?six'] # ↓↓ print(v.rsplit(sep='--')) # ['one-two', 'three-', 'four-=*!five-=*!?six'] # ↓↓↓ print(v.rsplit(sep='---')) # ['one-two--three', 'four-=*!five-=*!?six'] # ↓↓↓ print(v.rsplit(sep='-=*')) # ['one-two--three---four', '!five', '!?six']
v = '' print(v.rsplit()) # [] print(v.rsplit(sep=' ')) print(v.rsplit(sep='-')) # ['']
<Bytes & Bytearray>:
bytes:
v = b'one two three four\nfive\tsix' # ↑ ↑↑ ↑↑↑ print(v) # b'one two three four\nfive\tsix' print(v.rsplit()) print(v.rsplit(sep=None, maxsplit=-1)) # [b'one', b'two', b'three', b'four', b'five', b'six'] print(v.rsplit(maxsplit=0)) # [b'one two three four\nfive\tsix'] print(v.rsplit(maxsplit=1)) # [b'one two three four\nfive', b'six'] print(v.rsplit(maxsplit=2)) # [b'one two three four', b'five', b'six'] print(v.rsplit(maxsplit=3)) # [b'one two three', b'four', b'five', b'six'] print(v.rsplit(maxsplit=4)) # [b'one two', b'three', b'four', b'five', b'six'] print(v.rsplit(maxsplit=5)) # [b'one', b'two', b'three', b'four', b'five', b'six'] # ↓ print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) # [b'one', b'two', b'', b'three', b'', b'', b'four\nfive\tsix'] # ↓↓ print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) # [b'one two', b'three ', b'four\nfive\tsix'] # ↓↓↓ print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) # [b'one two three', b'four\nfive\tsix'] # ↓↓↓↓ print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) # [b'one two three four\nfive\tsix'] print(v.rsplit(sep=b'')) print(v.rsplit(sep=bytearray(b''))) # ValueError: empty separator
v = b'one-two--three---four-=*!five-=*!?six' # ↑ ↑↑ ↑↑↑ ↑↑↑↑ ↑↑↑↑↑ print(v) # b'one-two--three---four-=*!five-=*!?six' print(v.rsplit()) # [b'one-two--three---four-=*!five-=*!?six'] # ↓ print(v.rsplit(sep=b'-')) print(v.rsplit(sep=bytearray(b'-'))) # [b'one', b'two', b'', b'three', b'', b'', b'four', b'=*!five', b'=*!?six'] # ↓↓ print(v.rsplit(sep=b'--')) print(v.rsplit(sep=bytearray(b'--'))) # [b'one-two', b'three-', b'four-=*!five-=*!?six'] # ↓↓↓ print(v.rsplit(sep=b'---')) print(v.rsplit(sep=bytearray(b'---'))) # [b'one-two--three', b'four-=*!five-=*!?six'] # ↓↓↓ print(v.rsplit(sep=b'-=*')) print(v.rsplit(sep=bytearray(b'-=*'))) # [b'one-two--three---four', b'!five', b'!?six']
v = '' print(v.rsplit()) # [] print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) print(v.rsplit(sep=b'-')) print(v.rsplit(sep=bytearray(b'-'))) # [b'']
bytearray:
v = bytearray(b'one two three four\nfive\tsix') # ↑ ↑↑ ↑↑↑ print(v) # bytearray(b'one two three four\nfive\tsix') print(v.rsplit()) print(v.rsplit(sep=None, maxsplit=-1)) # [bytearray(b'one'), bytearray(b'two'), bytearray(b'three'), # bytearray(b'four'), bytearray(b'five'), bytearray(b'six')] print(v.rsplit(maxsplit=0)) # [bytearray(b'one two three four\nfive\tsix')] print(v.rsplit(maxsplit=1)) # [bytearray(b'one two three four\nfive'), bytearray(b'six')] print(v.rsplit(maxsplit=2)) # [bytearray(b'one two three four'), bytearray(b'five'), # bytearray(b'six')] print(v.rsplit(maxsplit=3)) # [bytearray(b'one two three'), bytearray(b'four'), bytearray(b'five'), # bytearray(b'six')] print(v.rsplit(maxsplit=4)) # [bytearray(b'one two'), bytearray(b'three'), bytearray(b'four'), # bytearray(b'five'), bytearray(b'six')] print(v.rsplit(maxsplit=5)) # [bytearray(b'one'), bytearray(b'two'), bytearray(b'three'), # bytearray(b'four'), bytearray(b'five'), bytearray(b'six')] # ↓ print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) # [bytearray(b'one'), bytearray(b'two'), bytearray(b''), # bytearray(b'three'), bytearray(b''), bytearray(b''), # bytearray(b'four\nfive\tsix')] # ↓↓ print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) # [bytearray(b'one two'), bytearray(b'three '), # bytearray(b'four\nfive\tsix')] # ↓↓↓ print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) # [bytearray(b'one two three'), bytearray(b'four\nfive\tsix')] # ↓↓↓↓ print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) # [bytearray(b'one two three four\nfive\tsix')] print(v.rsplit(sep=b'')) print(v.rsplit(sep=bytearray(b''))) # ValueError: empty separator
v = bytearray(b'one-two--three---four-=*!five-=*!?six') # ↑ ↑↑ ↑↑↑ ↑↑↑↑ ↑↑↑↑↑ print(v) # bytearray(b'one-two--three---four-=*!five-=*!?six') print(v.rsplit()) # [bytearray(b'one-two--three---four-=*!five-=*!?six')] # ↓ print(v.rsplit(sep=b'-')) print(v.rsplit(sep=bytearray(b'-'))) # [bytearray(b'one'), bytearray(b'two'), bytearray(b''), # bytearray(b'three'), bytearray(b''), bytearray(b''), # bytearray(b'four'), bytearray(b'=*!five'), bytearray(b'=*!?six')] # ↓↓ print(v.rsplit(sep=b'--')) print(v.rsplit(sep=bytearray(b'--'))) # [bytearray(b'one-two'), bytearray(b'three-'), # bytearray(b'four-=*!five-=*!?six')] # ↓↓↓ print(v.rsplit(sep=b'---')) print(v.rsplit(sep=bytearray(b'---'))) # [bytearray(b'one-two--three'), bytearray(b'four-=*!five-=*!?six')] # ↓↓↓ print(v.rsplit(sep=b'-=*')) print(v.rsplit(sep=bytearray(b'-=*'))) # [bytearray(b'one-two--three---four'), bytearray(b'!five'), # bytearray(b'!?six')]
v = bytearray(b'') print(v.rsplit()) # [] print(v.rsplit(sep=b' ')) print(v.rsplit(sep=bytearray(b' '))) print(v.rsplit(sep=b'-')) print(v.rsplit(sep=bytearray(b'-'))) # [bytearray(b'')]
Top comments (0)