DEV Community

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

Posted on • Edited on

partition in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

str.partition() and bytes.partition() or bytearray.partition() can split the string and bytes or bytearray respectively at the 1st occurrence of a separator, searching from the left to the right as shown below:

*Memo:

  • The 1st argument is sep(Required-Type:str for str.partition() or Bytes-like object for bytes.partition() and bytearray.partition()):
    • It's the separator of the one or more characters and bytes to separate a string and bytes or bytearray respectively.
    • An empty string and bytes or bytearray cannot be set respectively.
    • Don't use sep=.
  • It returns a tuple of 3 elements.
  • If sep isn't found, a tuple of the original string and two empty strings in order and the original bytes or bytearray and two empty bytes or bytearray in order are returned as 3 elements respectively.

<String>:

v = "It's very very good"

print(v.partition('er'))
# ("It's v", 'er', 'y very good')

print(v.partition('very'))
# ("It's ", 'very', ' very good')

                 # ↓
print(v.partition(' '))
# ("It's", ' ', 'very very good')

                 # ↓↓
print(v.partition('  '))
print(v.partition('ER'))
print(v.partition('VERY'))
print(v.partition('really'))
# ("It's very very good", '', '')

print(v.partition(''))
# ValueError: empty separator
Enter fullscreen mode Exit fullscreen mode
v = ''

print(v.partition('er'))
# ('', '', '')
Enter fullscreen mode Exit fullscreen mode

<Bytes & Bytearray>:

bytes:

v = b"It's very very good"

print(v.partition(b'er'))
# (b"It's v", b'er', b'y very good')

print(v.partition(bytearray(b'er')))
# (b"It's v", bytearray(b'er'), b'y very good')

print(v.partition(b'very'))
# (b"It's ", b'very', b' very good')

print(v.partition(bytearray(b'very')))
# (b"It's ", bytearray(b'very'), b' very good')

                  # ↓
print(v.partition(b' '))
# (b"It's", b' ', b'very very good')

print(v.partition(bytearray(b' ')))
# (b"It's", bytearray(b' '), b'very very good')

                 # ↓↓
print(v.partition(b'  '))
print(v.partition(bytearray(b'  ')))
print(v.partition(b'ER'))
print(v.partition(bytearray(b'ER')))
print(v.partition(b'VERY'))
print(v.partition(bytearray(b'VERY')))
print(v.partition(b'really'))
print(v.partition(bytearray(b'really')))
# ("It's very very good", '', '')

print(v.partition(b''))
print(v.partition(bytearray(b'')))
# ValueError: empty separator
Enter fullscreen mode Exit fullscreen mode
v = b''

print(v.partition(b'er'))
print(v.partition(bytearray(b'er')))
# (b'', b'', b'')
Enter fullscreen mode Exit fullscreen mode

bytearray:

v = bytearray(b"It's very very good")

print(v.partition(b'er'))
print(v.partition(bytearray(b'er')))
# (bytearray(b"It\'s v"), bytearray(b'er'), bytearray(b'y very good'))

print(v.partition(b'very'))
print(v.partition(bytearray(b'very')))
# (bytearray(b"It\'s "), bytearray(b'very'), bytearray(b' very good'))

                  # ↓
print(v.partition(b' '))
print(v.partition(bytearray(b' ')))
# (bytearray(b"It\'s"), bytearray(b' '), bytearray(b'very very good'))

                  # ↓↓
print(v.partition(b'  '))
print(v.partition(bytearray(b'  ')))
print(v.partition(b'ER'))
print(v.partition(bytearray(b'ER')))
print(v.partition(b'VERY'))
print(v.partition(bytearray(b'VERY')))
print(v.partition(b'really'))
print(v.partition(bytearray(b'really')))
# (bytearray(b"It\'s very very good"), bytearray(b''), bytearray(b''))

print(v.partition(b''))
print(v.partition(bytearray(b'')))
# ValueError: empty separator
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'')

print(v.partition(b'er'))
print(v.partition(bytearray(b'er')))
# (bytearray(b''), bytearray(b''), bytearray(b''))
Enter fullscreen mode Exit fullscreen mode

Top comments (0)