*Memo for a string and byte string(bytes
and bytearray
) functions:
- My post explains replace().
- My post explains removeprefix() and removesuffix().
- My post explains endswith().
*Memo for a string and byte string(bytes
and bytearray
):
str.startswith() and bytes.startswith() or bytearray.startswith() can check if prefix
is the prefix of the string and byte string respectively between [start, end)
as shown below:
*Memo:
- The 1st argument is
prefix
(Required-Type:str
andtuple(str)
forstr.startswith()
or Bytes-like object andtyple(Bytes-like object)
forbytes.startswith()
andbytearray.startswith()
):- It's the prefix of zero or more characters and bytes respectively.
- If it's an empty string and byte string respectively,
True
is returned. - Don't use
prefix=
.
- The 2nd argument is
start
(Optional-Type:int
or NoneType):- It's a start index(inclusive).
- If it's not set or
None
,0
is set to it. - Don't use
start
.
- The 3rd argument is
end
(Optional-Type:int
or NoneType):- It's an end index(exclusive).
- If it's not set or
None
, the length+ 1
is set to it. - Don't use
end
.
<String>:
v = 'hello world'
print(v.startswith('he')) # True
print(v.startswith('he', None, None)) # True
print(v.startswith('HE')) # False
print(v.startswith('wo')) # False
print(v.startswith(('he', 'wo'))) # True
print(v.startswith(('wo', 'he'))) # True
print(v.startswith(('wo', 'lo'))) # False
print(v.startswith('he', 0)) # True
print(v.startswith('he', 1)) # False
print(v.startswith('he', 0, 1)) # False
print(v.startswith('he', 0, 2)) # True
print(v.startswith('wo', 6, 7)) # False
print(v.startswith('wo', 6, 8)) # True
print(v.startswith('')) # True
v = ''
print(v.startswith('')) # True
<Byte String(bytes & bytearray)>:
v = b'hello world'
v = bytearray(b'hello world')
print(v.startswith(b'he')) # True
print(v.startswith(bytearray(b'he'))) # True
print(v.startswith(b'he', None, None)) # True
print(v.startswith(bytearray(b'he'), None, None)) # True
print(v.startswith(b'HE')) # False
print(v.startswith(bytearray(b'HE'))) # False
print(v.startswith(b'wo')) # False
print(v.startswith(bytearray(b'wo'))) # False
print(v.startswith((b'he', b'wo'))) # True
print(v.startswith((bytearray(b'he'), bytearray(b'wo')))) # True
print(v.startswith((b'wo', b'he'))) # True
print(v.startswith((bytearray(b'wo'), bytearray(b'he')))) # True
print(v.startswith((b'wo', b'lo'))) # False
print(v.startswith((bytearray(b'wo'), bytearray(b'lo')))) # False
print(v.startswith(b'he', 0)) # True
print(v.startswith(bytearray(b'he'), 0)) # True
print(v.startswith(b'he', 1)) # False
print(v.startswith(bytearray(b'he'), 1)) # False
print(v.startswith(b'he', 0, 1)) # False
print(v.startswith(bytearray(b'he'), 0, 1)) # False
print(v.startswith(b'he', 0, 2)) # True
print(v.startswith(bytearray(b'he'), 0, 2)) # True
print(v.startswith(b'wo', 6, 7)) # False
print(v.startswith(bytearray(b'wo'), 6, 7)) # False
print(v.startswith(b'wo', 6, 8)) # True
print(v.startswith(bytearray(b'wo'), 6, 8)) # True
print(v.startswith(b'')) # True
print(v.startswith(bytearray(b''))) # True
v = b''
v = bytearray(b'')
print(v.startswith(b''))
print(v.startswith(bytearray(b'')))
# True
Top comments (0)