*Memo:
- My post explains string, bytes and bytearray functions.
str.startswith() and bytes.startswith() or bytearray.startswith() can check if prefix is the prefix of the string and bytes or bytearray respectively in the range [start, end) as shown below:
*Memo:
- The 1st argument is
prefix(Required-Type:strandtuple(str)forstr.startswith()or Bytes-like object andtuple(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 bytes or bytearray respectively,
Trueis returned. - Don't use
prefix=.
- The 2nd argument is
start(Optional-Default:None-Type:int/NoneType):- It's a start index(inclusive).
- If it's
None, the 1st index is set to it. - Don't use
start.
- The 3rd argument is
end(Optional-Default:None-Type:int/NoneType):- It's an end index(exclusive).
- If it's
None, the string and bytes or bytearray length are set to it respectively. - Don't use
end.
-
startandendcan be signed indices(zero and positive and negative indices). - Error doesn't occur even if
[start, end)is out of the range[The 1st index, The string and bytes or bytearray length).
<String>:
v = 'hello world'
print(v.startswith('he')) # True
print(v.startswith('he', None, None)) # True
print(v.startswith('he', -100, 100)) # 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', -11)) # True
print(v.startswith('he', 1)) # False
print(v.startswith('he', -10)) # False
print(v.startswith('he', 0, 1)) # False
print(v.startswith('he', -11, -10)) # False
print(v.startswith('he', 0, 2)) # True
print(v.startswith('he', -11, -9)) # True
print(v.startswith('wo', 6, 7)) # False
print(v.startswith('wo', -5, -4)) # False
print(v.startswith('wo', 6, 8)) # True
print(v.startswith('wo', -5, -3)) # True
print(v.startswith('')) # True
v = ''
print(v.startswith('')) # True
<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', -100, 100)) # True
print(v.startswith(bytearray(b'he'), -100, 100)) # 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(b'he', -11)) # True
print(v.startswith(bytearray(b'he'), 0)) # True
print(v.startswith(bytearray(b'he'), -11)) # True
print(v.startswith(b'he', 1)) # False
print(v.startswith(b'he', -10)) # False
print(v.startswith(bytearray(b'he'), 1)) # False
print(v.startswith(bytearray(b'he'), -10)) # False
print(v.startswith(b'he', 0, 1)) # False
print(v.startswith(b'he', -11, -10)) # False
print(v.startswith(bytearray(b'he'), 0, 1)) # False
print(v.startswith(bytearray(b'he'), -11, -10)) # False
print(v.startswith(b'he', 0, 2)) # True
print(v.startswith(b'he', -11, -9)) # True
print(v.startswith(bytearray(b'he'), 0, 2)) # True
print(v.startswith(bytearray(b'he'), -11, -9)) # True
print(v.startswith(b'wo', 6, 7)) # False
print(v.startswith(b'wo', -5, -4)) # False
print(v.startswith(bytearray(b'wo'), 6, 7)) # False
print(v.startswith(bytearray(b'wo'), -5, -4)) # False
print(v.startswith(b'wo', 6, 8)) # True
print(v.startswith(b'wo', -5, -3)) # True
print(v.startswith(bytearray(b'wo'), 6, 8)) # True
print(v.startswith(bytearray(b'wo'), -5, -3)) # 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)