*Memo for string, bytes and bytearray functions:
- My post explains replace().
- My post explains startswith().
- My post explains endswith().
*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
forstr.removeprefix()
or Bytes-like object forbytes.removeprefix()
andbytearray.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
<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'
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')
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
forstr.removesuffix()
or Bytes-like object forbytes.removesuffix()
andbytearray.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
<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'
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')
Top comments (0)