removeprefix & removesuffix in Python
Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) @hyperkai

About: I'm a web developer. Buy Me a Coffee: ko-fi.com/superkai SO: stackoverflow.com/users/3247006/super-kai-kazuya-ito X(Twitter): twitter.com/superkai_kazuya FB: facebook.com/superkai.kazuya

Joined:
Oct 21, 2021

removeprefix & removesuffix in Python

Publish Date: Aug 4
0 0

Buy Me a Coffee

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

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

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

*Memos

  • 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

<Byte String>:

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 byte string respectively as shown below:

*Memos

  • 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

<Byte String>:

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

Comments 0 total

    Add comment