*Memos:
My post explains count() with a string and byte string.
My post explains rfind() with a string and byte string.
My post explains index() with a string and byte string.
My post explains rindex() with a string and byte string.
My post explains encode() and decode() with a string and byte string.
My post explains a string.
str.find() and bytes.find() or bytearray.find() can find the 1st substring and byte substring respectively, searching from the left to the right to return the index (without error even if the substring and byte substring respectively aren't found) as shown below:
*Memos:
- The 1st argument is
sub(Required-Type:strforstr.find()or bytes-like object forbytes.find()andbytearray.find()):
- It's the substring or byte substring of zero or more characters.
- Don't use
sub=.
- The 2nd argument is
start(Optional-Type:intorNoneType):
- It's a start index.
- If it's not set or
None,0is set. - Don't use
start.
- The 3rd argument is
end(Optional-Type:intorNoneType):
- It's an end index.
- If it's not set or
None, the length+ 1is set. - Don't use
end.
-1is returned if the substring or byte substring isn't found.
<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
v = ''
print(v.find('')) # 0
<Byte String(UTF-8)>:
bytes() & bytearray():
v = 'grapes peach'.encode()
v = b'grapes peach'
v = bytearray('grapes peach'.encode())
print(v.find('pe'.encode())) # 3
print(v.find(b'pe')) # 3
print(v.find(bytearray('pe'.encode()))) # 3
print(v.find('pe'.encode(), None, None)) # 3
print(v.find(b'pe', None, None)) # 3
print(v.find(bytearray('pe'.encode()), None, None)) # 3
print(v.find('PE'.encode())) # -1
print(v.find(b'PE')) # -1
print(v.find(bytearray('PE'.encode()))) # -1
print(v.find('pe'.encode(), 3)) # 3
print(v.find(b'pe', 3)) # 3
print(v.find(bytearray('pe'.encode()), 3)) # 3
print(v.find('pe'.encode(), 4)) # 7
print(v.find(b'pe', 4)) # 7
print(v.find(bytearray('pe'.encode()), 4)) # 7
print(v.find('pe'.encode(), 7)) # 7
print(v.find(b'pe', 7)) # 7
print(v.find(bytearray('pe'.encode()), 7)) # 7
print(v.find('pe'.encode(), 8)) # -1
print(v.find(b'pe', 8)) # -1
print(v.find(bytearray('pe'.encode()), 8)) # -1
print(v.find('pe'.encode(), 0, 4)) # -1
print(v.find(b'pe', 0, 4)) # -1
print(v.find(bytearray('pe'.encode()), 0, 4)) # -1
print(v.find('pe'.encode(), 0, 5)) # 3
print(v.find(b'pe', 0, 5)) # 3
print(v.find(bytearray('pe'.encode()), 0, 5)) # 3
print(v.find('pe'.encode(), 7, 12)) # 7
print(v.find(b'pe', 7, 12)) # 7
print(v.find(bytearray('pe'.encode()), 7, 12)) # 7
print(v.find('pe'.encode(), 8, 12)) # -1
print(v.find(b'pe', 8, 12)) # -1
print(v.find(bytearray('pe'.encode()), 8, 12)) # -1
print(v.find(''.encode())) # 0
print(v.find(b'')) # 0
print(v.find(bytearray(''.encode()))) # 0
print(v.find('g'.encode(), 0, 1)) # 0
print(v.find(b'g', 0, 1)) # 0
print(v.find(bytearray('g'.encode()), 0, 1)) # 0
print(v.find('h'.encode(), 11, 12)) # 11
print(v.find(b'h', 11, 12)) # 11
print(v.find(bytearray('h'.encode()), 11, 12)) # 11
print(v.find('abc'.encode())) # -1
print(v.find(b'abc')) # -1
print(v.find(bytearray('abc'.encode()))) # -1
v = ''.encode()
v = b''
v = bytearray(''.encode())
print(v.find(''.encode())) # 0
print(v.find(b'')) # 0
print(v.find(bytearray(''.encode()))) # 0