*Memos for a string and byte string(bytes
and bytearray
) functions:
- My post explains replace().
- My post explains removeprefix() and removesuffix().
- My post explains startswith().
*Memos for a string and byte string(bytes
and bytearray
):
str.endswith() and bytes.endswith() or bytearray.endswith() can check if suffix
is the suffix of a string and byte string respectively as shown below:
*Memos:
- The 1st argument is
suffix
(Required-Type:str
andtuple(str)
forstr.endswith()
or bytes-like object andtuple(bytes-like object)
forbytes.endswith()
andbytearray.endswith()
):- It's the suffix of zero or more characters.
- If it's an empty string or byte string,
True
is returned. - Don't use
suffix=
.
- The 2nd argument is
start
(Optional-Type:int
orNoneType
):- It's a start index.
- If it's not set or
None
,0
is set. - Don't use
start
.
- The 3rd argument is
end
(Optional-Type:int
orNoneType
):- It's an end index.
- If it's not set or
None
, the length+ 1
is set. - Don't use
end
.
<String>:
v = 'hello world'
print(v.endswith('ld')) # True
print(v.endswith('ld', None, None)) # True
print(v.endswith('LD')) # False
print(v.endswith('lo')) # False
print(v.endswith(('ld', 'lo'))) # True
print(v.endswith(('lo', 'ld'))) # True
print(v.endswith(('lo', 'wo'))) # False
print(v.endswith('ld', 9)) # True
print(v.endswith('ld', 10)) # False
print(v.endswith('ld', 9, 10)) # False
print(v.endswith('ld', 9, 11)) # True
print(v.endswith('lo', 3, 4)) # False
print(v.endswith('lo', 3, 5)) # True
print(v.endswith('')) # True
v = ''
print(v.endswith('')) # True
<Byte String(bytes & bytearray)>:
v = b'hello world'
v = bytearray(b'hello world')
print(v.endswith(b'ld')) # True
print(v.endswith(bytearray(b'ld'))) # True
print(v.endswith(b'ld', None, None)) # True
print(v.endswith(bytearray(b'ld'), None, None)) # True
print(v.endswith(b'LD')) # False
print(v.endswith(bytearray(b'LD'))) # False
print(v.endswith(b'lo')) # False
print(v.endswith(bytearray(b'lo'))) # False
print(v.endswith((b'ld', b'lo'))) # True
print(v.endswith((bytearray(b'ld'), bytearray(b'lo')))) # True
print(v.endswith((b'lo', b'ld'))) # True
print(v.endswith((bytearray(b'lo'), bytearray(b'ld')))) # True
print(v.endswith((b'lo', b'wo'))) # False
print(v.endswith((bytearray(b'lo'), bytearray(b'wo')))) # False
print(v.endswith(b'ld', 9)) # True
print(v.endswith(bytearray(b'ld'), 9)) # True
print(v.endswith(b'ld', 10)) # False
print(v.endswith(bytearray(b'ld'), 10)) # False
print(v.endswith(b'ld', 9, 10)) # False
print(v.endswith(bytearray(b'ld'), 9, 10)) # False
print(v.endswith(b'ld', 9, 11)) # True
print(v.endswith(bytearray(b'ld'), 9, 11)) # True
print(v.endswith(b'lo', 3, 4)) # False
print(v.endswith(bytearray(b'lo'), 3, 4)) # False
print(v.endswith(b'lo', 3, 5)) # True
print(v.endswith(bytearray(b'lo'), 3, 5)) # True
print(v.endswith(b'')) # True
print(v.endswith(bytearray(b''))) # True
v = b''
v = bytearray(b'')
print(v.endswith(b'')) # True
print(v.endswith(bytearray(b''))) # True