DEV Community

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

Posted on • Edited on

removeprefix & removesuffix in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

str.removeprefix() and bytes.removeprefix() or bytearray.removeprefix() can remove the prefix of the string and bytes or bytearray respectively as shown below:

*Memo:

  • The 1st argument is prefix(Required-Type:str for str.removeprefix() or Bytes-like object for bytes.removeprefix() and bytearray.removeprefix()):
    • It's the prefix of zero or more characters and bytes respectively.
    • Don't use prefix=.

<String>:

v = 'hello world'

print(v.removeprefix('he'))
# llo world

print(v.removeprefix('hello '))
# world

print(v.removeprefix('hello wo'))
# rld

print(v.removeprefix('HE'))
print(v.removeprefix(''))
print(v.removeprefix('abc'))
# hello world
Enter fullscreen mode Exit fullscreen mode

<Bytes & Bytearray>:

bytes:

v = b'hello world'

print(v.removeprefix(b'he'))
print(v.removeprefix(bytearray(b'he')))
# b'llo world'

print(v.removeprefix(b'hello '))
print(v.removeprefix(bytearray(b'hello ')))
# b'world'

print(v.removeprefix(b'hello wo'))
print(v.removeprefix(bytearray(b'hello wo')))
# b'rld'

print(v.removeprefix(b'HE'))
print(v.removeprefix(bytearray(b'HE')))
print(v.removeprefix(b''))
print(v.removeprefix(bytearray(b'')))
print(v.removeprefix(b'abc'))
print(v.removeprefix(bytearray(b'abc')))
# b'hello world'
Enter fullscreen mode Exit fullscreen mode

bytearray:

v = bytearray(b'hello world')

print(v.removeprefix(b'he'))
print(v.removeprefix(bytearray(b'he')))
# bytearray(b'llo world')

print(v.removeprefix(b'hello '))
print(v.removeprefix(bytearray(b'hello ')))
# bytearray(b'world')

print(v.removeprefix(b'hello wo'))
print(v.removeprefix(bytearray(b'hello wo')))
# bytearray(b'rld')

print(v.removeprefix(b'HE'))
print(v.removeprefix(bytearray(b'HE')))
print(v.removeprefix(b''))
print(v.removeprefix(bytearray(b'')))
print(v.removeprefix(b'abc'))
print(v.removeprefix(bytearray(b'abc')))
# bytearray(b'hello world')
Enter fullscreen mode Exit fullscreen mode

str.removesuffix() and bytes.removesuffix() or bytearray.removesuffix() can remove the suffix of the string and bytes or bytearray respectively as shown below:

*Memo:

  • The 1st argument is suffix(Required-Type:str for str.removesuffix() or Bytes-like object for bytes.removesuffix() and bytearray.removesuffix()):
    • It's the suffix of zero or more characters and bytes respectively.
    • Don't use suffix=.

<String>:

v = 'hello world'

print(v.removesuffix('ld'))
# hello wor

print(v.removesuffix(' world'))
# hello

print(v.removesuffix('lo world'))
# hel

print(v.removesuffix('LD'))
print(v.removesuffix(''))
print(v.removesuffix('abc'))
# hello world
Enter fullscreen mode Exit fullscreen mode

<Bytes & Bytearray>:

bytes:

v = b'hello world'

print(v.removesuffix(b'ld'))
print(v.removesuffix(bytearray(b'ld')))
# b'hello wor'

print(v.removesuffix(b' world'))
print(v.removesuffix(bytearray(b' world')))
# b'hello'

print(v.removesuffix(b'lo world'))
print(v.removesuffix(bytearray(b'lo world')))
# b'hel'

print(v.removesuffix(b'LD'))
print(v.removesuffix(bytearray(b'LD')))
print(v.removesuffix(b''))
print(v.removesuffix(bytearray(b'')))
print(v.removesuffix(b'abc'))
print(v.removesuffix(bytearray(b'abc')))
# b'hello world'
Enter fullscreen mode Exit fullscreen mode

bytearray:

v = bytearray(b'hello world')

print(v.removesuffix(b'ld'))
print(v.removesuffix(bytearray(b'ld')))
# bytearray(b'hello wor')

print(v.removesuffix(b' world'))
print(v.removesuffix(bytearray(b' world')))
# bytearray(b'hello')

print(v.removesuffix(b'lo world'))
print(v.removesuffix(bytearray(b'lo world')))
# bytearray(b'hel')

print(v.removesuffix(b'LD'))
print(v.removesuffix(bytearray(b'LD')))
print(v.removesuffix(b''))
print(v.removesuffix(bytearray(b'')))
print(v.removesuffix(b'abc'))
print(v.removesuffix(bytearray(b'abc')))
# bytearray(b'hello world')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)