DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

rsplit in Python

Buy Me a Coffee

*Memo for a string and byte string(bytes and bytearray) functions:

*Memo for a string and byte string(bytes and bytearray):

str.rsplit() and bytes.rsplit() or bytearray.rsplit() can split the string and byte string respectively from the right to the left as shown below:

*Memo:

  • The 1st argument is sep(Optional-Default:None-Type:str for str.rsplit(), Bytes-like object for bytes.rsplit() and bytearray.rsplit() or NoneType):
    • It's the delimiter of the one or more characters and bytes to delimit a string and byte string respectively.
    • An empty string and byte string 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 byte strings respectively:
    • For example, '1,,,2'.rsplit(sep=',') returns ['1', '', '', '2'] and b'1,,,2'.rsplit(sep=b',') returns [b'1', b'', b'', b'2'].

<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
Enter fullscreen mode Exit fullscreen mode
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']
Enter fullscreen mode Exit fullscreen mode
v = ''

print(v.rsplit()) # []

print(v.rsplit(sep=' '))
print(v.rsplit(sep='-'))
# ['']
Enter fullscreen mode Exit fullscreen mode

<Byte String>:

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
Enter fullscreen mode Exit fullscreen mode
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']
Enter fullscreen mode Exit fullscreen mode
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'']
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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')]
Enter fullscreen mode Exit fullscreen mode
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'')]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)