DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

startswith in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

str.startswith() and bytes.startswith() or bytearray.startswith() can check if prefix is the prefix of the string and bytes or bytearray respectively between [start, end) as shown below:

*Memo:

  • The 1st argument is prefix(Required-Type:str and tuple(str) for str.startswith() or Bytes-like object and typle(Bytes-like object) for bytes.startswith() and bytearray.startswith()):
    • It's the prefix of zero or more characters and bytes respectively.
    • If it's an empty string and bytes or bytearray 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 
Enter fullscreen mode Exit fullscreen mode
v = '' print(v.startswith('')) # True 
Enter fullscreen mode Exit fullscreen mode

<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 
Enter fullscreen mode Exit fullscreen mode
v = b'' v = bytearray(b'') print(v.startswith(b'')) print(v.startswith(bytearray(b''))) # True 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)