*Memo for a string and byte string(bytes
and bytearray
) functions:
- My post explains count().
- My post explains index().
- My post explains rindex().
- My post explains rfind().
- My post explains ord(), sorted() and reversed().
*Memo for a string and byte 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
forstr.find()
or Bytes-like object andint
forbytes.find()
andbytearray.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 ifsub
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
v = ''
print(v.find('')) # 0
<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
v = b''
v = bytearray(b'')
print(v.find(b'')) # 0
print(v.find(bytearray(b''))) # 0
Top comments (0)