*Memo for string, bytes and bytearray functions:
- My post explains count().
- My post explains rindex().
- My post explains find().
- My post explains rfind().
- My post explains ord(), sorted() and reversed().
*Memo for a string, bytes and bytearray:
str.index() and bytes.index() or bytearray.index() 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 (with error if the substring and byte substring don't exist respectively) as shown below:
*Memo:
- The 1st argument is
sub
(Required-Type:str
forstr.index()
or Bytes-like object andint
forbytes.index()
andbytearray.index()
):- 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
.
- Error occurs if
sub
doesn't exist.
<String>:
v = 'grapes peach' print(v.index('pe')) # 3 print(v.index('pe', None, None)) # 3 print(v.index('pe', 3)) # 3 print(v.index('pe', 4)) # 7 print(v.index('pe', 7)) # 7 print(v.index('pe', 0, 5)) # 3 print(v.index('pe', 7, 12)) # 7 print(v.index('')) # 0 print(v.index('g', 0, 1)) # 0 print(v.index('h', 11, 12)) # 11 print(v.index('PE')) print(v.index('pe', 8)) print(v.index('pe', 0, 4)) print(v.index('pe', 8, 12)) print(v.index('abc')) # ValueError: substring not found
v = '' print(v.index('')) # 0
<Bytes & Bytearray>:
v = b'grapes peach' v = bytearray('grapes peach'.encode()) print(v.index(b'pe')) # 3 print(v.index(bytearray(b'pe'))) # 3 print(v.index(b'pe', None, None)) # 3 print(v.index(bytearray(b'pe'), None, None)) # 3 print(v.index(b'pe', 3)) # 3 print(v.index(bytearray(b'pe'), 3)) # 3 print(v.index(b'pe', 4)) # 7 print(v.index(bytearray(b'pe'), 4)) # 7 print(v.index(b'pe', 7)) # 7 print(v.index(bytearray(b'pe'), 7)) # 7 print(v.index(b'pe', 0, 5)) # 3 print(v.index(bytearray(b'pe'), 0, 5)) # 3 print(v.index(b'pe', 7, 12)) # 11 print(v.index(bytearray(b'pe'), 7, 12)) # 11 print(v.index(b'')) # 0 print(v.index(bytearray(b''))) # 0 print(v.index(b'g', 0, 1)) # 0 print(v.index(ord('g'), 0, 1)) # 0 print(v.index(103, 0, 1)) # 0 print(v.index(bytearray(b'g'), 0, 1)) # 0 print(v.index(b'h', 11, 12)) # 11 print(v.index(ord('h'), 11, 12)) # 11 print(v.index(104, 11, 12)) # 11 print(v.index(bytearray(b'h'), 11, 12)) # 11 print(v.index(b'PE')) print(v.index(bytearray(b'PE'))) print(v.index(b'pe', 8)) print(v.index(bytearray(b'pe'), 8)) print(v.index(b'pe', 0, 4)) print(v.index(bytearray(b'pe'), 0, 4)) print(v.index(b'pe', 8, 12)) print(v.index(bytearray(b'pe'), 8, 12)) print(v.index(b'abc')) print(v.index(bytearray(b'abc'))) # ValueError: subsection not found
v = b'' v = bytearray(b'') print(v.index(b'')) # 0 print(v.index(bytearray(b''))) # 0
Top comments (0)