DEV Community

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

Posted on • Edited on

find in Python

Buy Me a Coffee

*Memo for string, bytes and bytearray functions:

*Memo for a string, bytes and bytearray:

str.find() and bytes.find() or bytearray.find() can get the 1st index of the substring and byte substring respectively matched to sub between [start, end), searching from the left to the right (without error even if the substring and byte substring don't exist respectively) as shown below:

*Memo:

  • The 1st argument is sub(Required-Type:str for str.find() or Bytes-like object and int for bytes.find() and bytearray.find()):
    • It's the substring and byte substring of zero or more characters and bytes respectively.
    • Don't use sub=.
  • 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.
  • -1 is returned if sub doesn't exist.

<String>:

v = 'grapes peach' print(v.find('pe')) # 3 print(v.find('pe', None, None)) # 3 print(v.find('PE')) # -1 print(v.find('pe', 3)) # 3 print(v.find('pe', 4)) # 7 print(v.find('pe', 7)) # 7 print(v.find('pe', 8)) # -1 print(v.find('pe', 0, 4)) # -1 print(v.find('pe', 0, 5)) # 3 print(v.find('pe', 7, 12)) # 7 print(v.find('pe', 8, 12)) # -1  print(v.find('')) # 0 print(v.find('g', 0, 1)) # 0 print(v.find('h', 11, 12)) # 11 print(v.find('abc')) # -1 
Enter fullscreen mode Exit fullscreen mode
v = '' print(v.find('')) # 0 
Enter fullscreen mode Exit fullscreen mode

<Byte String(bytes & bytearray)>:

v = b'grapes peach' v = bytearray(b'grapes peach') print(v.find(b'pe')) # 3 print(v.find(bytearray(b'pe'))) # 3 print(v.find(b'pe', None, None)) # 3 print(v.find(bytearray(b'pe'), None, None)) # 3 print(v.find(b'PE')) # -1 print(v.find(bytearray(b'PE'))) # -1 print(v.find(b'pe', 3)) # 3 print(v.find(bytearray(b'pe'), 3)) # 3 print(v.find(b'pe', 4)) # 7 print(v.find(bytearray(b'pe'), 4)) # 7 print(v.find(b'pe', 7)) # 7 print(v.find(bytearray(b'pe'), 7)) # 7 print(v.find(b'pe', 8)) # -1 print(v.find(bytearray(b'pe'), 8)) # -1 print(v.find(b'pe', 0, 4)) # -1 print(v.find(bytearray(b'pe'), 0, 4)) # -1 print(v.find(b'pe', 0, 5)) # 3 print(v.find(bytearray(b'pe'), 0, 5)) # 3 print(v.find(b'pe', 7, 12)) # 7 print(v.find(bytearray(b'pe'), 7, 12)) # 7 print(v.find(b'pe', 8, 12)) # -1 print(v.find(bytearray(b'pe'), 8, 12)) # -1  print(v.find(b'')) # 0 print(v.find(bytearray(b''))) # 0 print(v.find(b'g', 0, 1)) # 0 print(v.find(ord('g'), 0, 1)) # 0 print(v.find(103, 0, 1)) # 0 print(v.find(bytearray(b'g'), 0, 1)) # 0 print(v.find(b'h', 11, 12)) # 11 print(v.find(ord('h'), 11, 12)) # 11 print(v.find(104, 11, 12)) # 11 print(v.find(bytearray(b'h'), 11, 12)) # 11 print(v.find(b'abc')) # -1 print(v.find(bytearray(b'abc'))) # -1 
Enter fullscreen mode Exit fullscreen mode
v = b'' v = bytearray(b'') print(v.find(b'')) # 0 print(v.find(bytearray(b''))) # 0 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)