*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 find().
- My post explains ord(), sorted() and reversed().
*Memo for a string and byte string(bytes
and bytearray
):
str.rfind() and bytes.rfind() or bytearray.rfind() can get the 1st index of the substring and byte substring respectively matched to sub
between [start, end)
, searching from the right to the left (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.rfind()
or Bytes-like object andint
forbytes.rfind()
andbytearray.rfind()
):- 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.
- If it's not set or
None
,0
is set. - Don't use
start
.
- The 3rd argument is
end
(Optional-Type:int
or NoneType):- It's an end index.
- If it's not set or
None
, the length+ 1
is set. - Don't use
end
.
-
-1
is returned ifsub
doesn't exist.
<String>:
v = 'grapes peach'
print(v.rfind('pe')) # 7
print(v.rfind('pe', None, None)) # 7
print(v.rfind('PE')) # -1
print(v.rfind('pe', 3)) # 7
print(v.rfind('pe', 4)) # 7
print(v.rfind('pe', 7)) # 7
print(v.rfind('pe', 8)) # -1
print(v.rfind('pe', 0, 4)) # -1
print(v.rfind('pe', 0, 5)) # 3
print(v.rfind('pe', 7, 12)) # 7
print(v.rfind('pe', 8, 12)) # -1
print(v.rfind('')) # 12
print(v.rfind('g', 0, 1)) # 0
print(v.rfind('h', 11, 12)) # 11
print(v.rfind('abc')) # -1
v = ''
print(v.rfind('')) # 0
<Byte String(bytes & bytearray)>:
v = b'grapes peach'
v = bytearray(b'grapes peach')
print(v.rfind(b'pe')) # 7
print(v.rfind(bytearray(b'pe')) # 7
print(v.rfind(b'pe', None, None)) # 7
print(v.rfind(bytearray(b'pe'), None, None)) # 7
print(v.rfind(b'PE')) # -1
print(v.rfind(bytearray(b'PE'))) # -1
print(v.rfind(b'pe', 3)) # 7
print(v.rfind(bytearray(b'pe'), 3)) # 7
print(v.rfind(b'pe', 4)) # 7
print(v.rfind(bytearray(b'pe'), 4)) # 7
print(v.rfind(b'pe', 7)) # 7
print(v.rfind(bytearray(b'pe'), 7)) # 7
print(v.rfind(b'pe', 8)) # -1
print(v.rfind(bytearray(b'pe'), 8)) # -1
print(v.rfind(b'pe', 0, 4)) # -1
print(v.rfind(bytearray(b'pe'), 0, 4)) # -1
print(v.rfind(b'pe', 0, 5)) # 3
print(v.rfind(bytearray(b'pe'), 0, 5)) # 3
print(v.rfind(b'pe', 7, 12)) # 7
print(v.rfind(bytearray(b'pe'), 7, 12)) # 7
print(v.rfind(b'pe', 8, 12)) # -1
print(v.rfind(bytearray(b'pe'), 8, 12)) # -1
print(v.rfind(b'')) # 12
print(v.rfind(bytearray(b''))) # 12
print(v.rfind(b'g', 0, 1)) # 0
print(v.rfind(ord('g'), 0, 1)) # 0
print(v.rfind(103, 0, 1)) # 0
print(v.rfind(bytearray(b'g'))) # 0
print(v.rfind(b'h', 11, 12)) # 11
print(v.rfind(ord('h'), 11, 12)) # 11
print(v.rfind(104, 11, 12)) # 11
print(v.rfind(bytearray(b'h'), 11, 12)) # 11
print(v.rfind(b'abc')) # -1
print(v.rfind(bytearray(b'abc'))) # -1
v = b''
v = bytearray(b'')
print(v.rfind(b'')) # 0
print(v.rfind(bytearray(b''))) # 0
Top comments (0)