DEV Community

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

Posted on • Edited on

split in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

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

*Memo:

  • The 1st argument is sep(Optional-Default:None-Type:str for str.split(), Bytes-like object for bytes.split() and bytearray.split() 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 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 bytes or bytearray respectively:
    • For example, '1,,,2'.split(sep=',') returns ['1', '', '', '2'] and b'1,,,2'.split(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.split())
print(v.split(sep=None, maxsplit=-1))
# ['one', 'two', 'three', 'four', 'five', 'six']

print(v.split(maxsplit=0))
# ['one two  three   four\nfive\tsix']

print(v.split(maxsplit=1))
# ['one', 'two  three   four\nfive\tsix']

print(v.split(maxsplit=2))
# ['one', 'two', 'three   four\nfive\tsix']

print(v.split(maxsplit=3))
# ['one', 'two', 'three', 'four\nfive\tsix']

print(v.split(maxsplit=4))
# ['one', 'two', 'three', 'four', 'five\tsix']

print(v.split(maxsplit=5))
# ['one', 'two', 'three', 'four', 'five', 'six']

                 # ↓
print(v.split(sep=' '))
# ['one', 'two', '', 'three', '', '', 'four\nfive\tsix']

                 # ↓↓
print(v.split(sep='  '))
# ['one two', 'three', ' four\nfive\tsix']

                 # ↓↓↓
print(v.split(sep='   '))
# ['one two  three', 'four\nfive\tsix']

                 # ↓↓↓↓
print(v.split(sep='    '))
# ['one two  three   four\nfive\tsix']

print(v.split(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.split())
# ['one-two--three---four-=*!five-=*!?six']

                 # ↓
print(v.split(sep='-'))
# ['one', 'two', '', 'three', '', '', 'four', '=*!five', '=*!?six']

                 # ↓↓
print(v.split(sep='--'))
# ['one-two', 'three', '-four-=*!five-=*!?six']

                 # ↓↓↓
print(v.split(sep='---'))
# ['one-two--three', 'four-=*!five-=*!?six']

                 # ↓↓↓
print(v.split(sep='-=*'))
# ['one-two--three---four', '!five', '!?six']
Enter fullscreen mode Exit fullscreen mode
v = ''

print(v.split()) # []

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

<Bytes & Bytearray>:

bytes:

v = b'one two  three   four\nfive\tsix'
       # ↑   ↑↑     ↑↑↑
print(v)
# b'one two  three   four\nfive\tsix'

print(v.split())
print(v.split(sep=None, maxsplit=-1))
# [b'one', b'two', b'three', b'four', b'five', b'six']

print(v.split(maxsplit=0))
# [b'one two  three   four\nfive\tsix']

print(v.split(maxsplit=1))
# [b'one', b'two  three   four\nfive\tsix']

print(v.split(maxsplit=2))
# [b'one', b'two', b'three   four\nfive\tsix']

print(v.split(maxsplit=3))
# [b'one', b'two', b'three', b'four\nfive\tsix']

print(v.split(maxsplit=4))
# [b'one', b'two', b'three', b'four', b'five\tsix']

print(v.split(maxsplit=5))
# [b'one', b'two', b'three', b'four', b'five', b'six']

                  # ↓
print(v.split(sep=b' '))
print(v.split(sep=bytearray(b' ')))
# [b'one', b'two', b'', b'three', b'', b'', b'four\nfive\tsix']

                  # ↓↓
print(v.split(sep=b'  '))
print(v.split(sep=bytearray(b'  ')))
# [b'one two', b'three', b' four\nfive\tsix']

                  # ↓↓↓
print(v.split(sep=b'   '))
print(v.split(sep=bytearray(b'   ')))
# [b'one two  three', b'four\nfive\tsix']

                  # ↓↓↓↓
print(v.split(sep=b'    '))
print(v.split(sep=bytearray(b'    ')))
# [b'one two  three   four\nfive\tsix']

print(v.split(sep=b''))
print(v.split(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.split())
# [b'one-two--three---four-=*!five-=*!?six']

                  # ↓
print(v.split(sep=b'-'))
print(v.split(sep=bytearray(b'-')))
# [b'one', b'two', b'', b'three', b'', b'', b'four', b'=*!five', b'=*!?six']
                  # ↓↓
print(v.split(sep=b'--'))
print(v.split(sep=bytearray(b'--')))
# [b'one-two', b'three', b'-four-=*!five-=*!?six']

                  # ↓↓↓
print(v.split(sep=b'---'))
print(v.split(sep=bytearray(b'---')))
# [b'one-two--three', b'four-=*!five-=*!?six']

                  # ↓↓↓
print(v.split(sep=b'-=*'))
print(v.split(sep=bytearray(b'-=*')))
# [b'one-two--three---four', b'!five', b'!?six']
Enter fullscreen mode Exit fullscreen mode
v = b''

print(v.split())
# []

print(v.split(sep=b' '))
print(v.split(sep=bytearray(b' ')))
print(v.split(sep=b'-'))
print(v.split(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.split())
print(v.split(sep=None, maxsplit=-1))
# [bytearray(b'one'), bytearray(b'two'), bytearray(b'three'),
#  bytearray(b'four'), bytearray(b'five'), bytearray(b'six')]

print(v.split(maxsplit=0))
# [bytearray(b'one two  three   four\nfive\tsix')]

print(v.split(maxsplit=1))
# [bytearray(b'one'), bytearray(b'two  three   four\nfive\tsix')]

print(v.split(maxsplit=2))
# [bytearray(b'one'), bytearray(b'two'),
#  bytearray(b'three   four\nfive\tsix')]

print(v.split(maxsplit=3))
# [bytearray(b'one'), bytearray(b'two'), bytearray(b'three'),
#  bytearray(b'four\nfive\tsix')]

print(v.split(maxsplit=4))
# [bytearray(b'one'), bytearray(b'two'), bytearray(b'three'),
#  bytearray(b'four'), bytearray(b'five\tsix')]

print(v.split(maxsplit=5))
# [bytearray(b'one'), bytearray(b'two'), bytearray(b'three'),
#  bytearray(b'four'), bytearray(b'five'), bytearray(b'six')]

                  # ↓
print(v.split(sep=b' '))
print(v.split(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.split(sep=b'  '))
print(v.split(sep=bytearray(b'  ')))
# [bytearray(b'one two'), bytearray(b'three'),
#  bytearray(b' four\nfive\tsix')]

                  # ↓↓↓
print(v.split(sep=b'   '))
print(v.split(sep=bytearray(b'   ')))
# [bytearray(b'one two  three'), bytearray(b'four\nfive\tsix')]

                  # ↓↓↓↓
print(v.split(sep=b'    '))
print(v.split(sep=bytearray(b'    ')))
# [bytearray(b'one two  three   four\nfive\tsix')]

print(v.split(sep=b''))
print(v.split(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.split())
# [bytearray(b'one-two--three---four-=*!five-=*!?six')]

                  # ↓
print(v.split(sep=b'-'))
print(v.split(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.split(sep=b'--'))
print(v.split(sep=bytearray(b'--')))
# [bytearray(b'one-two'), bytearray(b'three'),
#  bytearray(b'-four-=*!five-=*!?six')]

                  # ↓↓↓
print(v.split(sep=b'---'))
print(v.split(sep=bytearray(b'---')))
# [bytearray(b'one-two--three'), bytearray(b'four-=*!five-=*!?six')]

                  # ↓↓↓
print(v.split(sep=b'-=*'))
print(v.split(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.split())
# []

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

Top comments (0)