DEV Community

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

Posted on • Edited on

rindex in Python

Buy Me a Coffee

*Memo for a string and byte string(bytes and bytearray) functions:

*Memo for a string and byte string(bytes and bytearray):

str.rindex() and bytes.rindex() or bytearray.rindex() 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 to return the index (with error if the substring and byte substring don't exist respectively) as shown below:

*Memo:

  • The 1st argument is sub(Required-Type:str for str.rindex() or Bytes-like object and int for bytes.rindex() and bytearray.rindex()):
    • 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.rindex('pe'))             # 7
print(v.rindex('pe', None, None)) # 7
print(v.rindex('pe', 3))          # 7
print(v.rindex('pe', 7))          # 7
print(v.rindex('pe', 0, 5))       # 3
print(v.rindex('pe', 7, 12))      # 7

print(v.rindex(''))          # 12
print(v.rindex('g', 0, 1))   # 0
print(v.rindex('h', 11, 12)) # 11

print(v.rindex('PE'))
print(v.rindex('pe', 8))
print(v.rindex('pe', 0, 4))
print(v.rindex('pe', 8, 12))
print(v.rindex('abc'))
# ValueError: substring not found
Enter fullscreen mode Exit fullscreen mode
v = ''

print(v.rindex('')) # 0
Enter fullscreen mode Exit fullscreen mode

<Byte String(bytes & bytearray)>:

v = b'grapes peach'
v = bytearray('grapes peach'.encode())

print(v.rindex(b'pe'))                        # 7
print(v.rindex(bytearray(b'pe')))             # 7
print(v.rindex(b'pe', None, None))            # 7
print(v.rindex(bytearray(b'pe'), None, None)) # 7
print(v.rindex(b'pe', 3))                     # 7
print(v.rindex(bytearray(b'pe'), 3))          # 7
print(v.rindex(b'pe', 7))                     # 7
print(v.rindex(bytearray(b'pe'), 7))          # 7
print(v.rindex(b'pe', 0, 5))                  # 3
print(v.rindex(bytearray(b'pe'), 0, 5))       # 3
print(v.rindex(b'pe', 7, 12))                 # 7
print(v.rindex(bytearray(b'pe'), 7, 12))      # 7

print(v.rindex(b''))                     # 12
print(v.rindex(bytearray(b'')))          # 12
print(v.rindex(b'g', 0, 1))              # 0
print(v.rindex(ord('g'), 0, 1))          # 0
print(v.rindex(103, 0, 1))               # 0
print(v.rindex(bytearray(b'g'), 0, 1))   # 0
print(v.rindex(b'h', 11, 12))            # 11
print(v.rindex(ord('h'), 11, 12))        # 11
print(v.rindex(104, 11, 12))             # 11
print(v.rindex(bytearray(b'h'), 11, 12)) # 11

print(v.rindex(b'PE'))
print(v.rindex(bytearray(b'PE')))
print(v.rindex(b'pe', 8))
print(v.rindex(bytearray(b'pe'), 8))
print(v.rindex(b'pe', 0, 4))
print(v.rindex(bytearray(b'pe'), 0, 4))
print(v.rindex(b'pe', 8, 12))
print(v.rindex(bytearray(b'pe'), 8, 12))
print(v.rindex(b'abc'))
print(v.rindex(bytearray(b'abc')))
# ValueError: subsection not found
Enter fullscreen mode Exit fullscreen mode
v = b''
v = bytearray(''.encode())

print(v.rindex(b''))            # 0
print(v.rindex(bytearray(b''))) # 0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)