DEV Community

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

Posted on • Edited on

rindex in Python

Buy Me a Coffee

*Memo:

  • My post explains string, bytes and bytearray functions.

str.rindex() and bytes.rindex() or bytearray.rindex() can respectively get the last index of the substring and byte substring matched to sub in the range [start, end) (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-Default:None-Type:int/NoneType):
    • It's a start index(inclusive).
    • If it's None, the 1st index is set to it.
    • Don't use start.
  • The 3rd argument is end(Optional-Default:None-Type:int/NoneType):
    • It's an end index(exclusive).
    • If it's None, the string and bytes or bytearray length are set to it respectively.
    • Don't use end.
  • Error occurs if sub doesn't exist.
  • start and end can be signed indices(zero and positive and negative indices).
  • Error doesn't occur even if [start, end) is out of the range [The 1st index, The string and bytes or bytearray length).

<String>:

v = 'grapes peach'

print(v.rindex('pe'))             # 7
print(v.rindex('pe', None, None)) # 7
print(v.rindex('pe', -100, 100))  # 7
print(v.rindex('pe', 3))          # 7
print(v.rindex('pe', -9))         # 7
print(v.rindex('pe', 4))          # 7
print(v.rindex('pe', -8))         # 7
print(v.rindex('pe', 7))          # 7
print(v.rindex('pe', -5))         # 7
print(v.rindex('pe', 0, 5))       # 3
print(v.rindex('pe', -12, -7))    # 3
print(v.rindex('pe', 7, 12))      # 7
print(v.rindex('pe', -5, 12))     # 7

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

print(v.rindex('PE'))
print(v.rindex('pe', 8))
print(v.rindex('pe', -4))
print(v.rindex('pe', 0, 4))
print(v.rindex('pe', -12, -8))
print(v.rindex('pe', 8, 12))
print(v.rindex('pe', -4, 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

<Bytes & Bytearray>:

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

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', -100, 100))             # 7
print(v.rindex(bytearray(b'pe'), -100, 100))  # 7
print(v.rindex(b'pe', 3))                     # 7
print(v.rindex(bytearray(b'pe'), 3))          # 7
print(v.rindex(b'pe', -9))                    # 7
print(v.rindex(bytearray(b'pe'), -9))         # 7
print(v.rindex(b'pe', 4))                     # 7
print(v.rindex(bytearray(b'pe'), 4))          # 7
print(v.rindex(b'pe', -8))                    # 7
print(v.rindex(bytearray(b'pe'), -8))         # 7
print(v.rindex(b'pe', 7))                     # 7
print(v.rindex(bytearray(b'pe'), 7))          # 7
print(v.rindex(b'pe', -5))                    # 7
print(v.rindex(bytearray(b'pe'), -5))         # 7
print(v.rindex(b'pe', 0, 5))                  # 3
print(v.rindex(bytearray(b'pe'), 0, 5))       # 3
print(v.rindex(b'pe', -12, -7))               # 3
print(v.rindex(bytearray(b'pe'), -12, -7))    # 3
print(v.rindex(b'pe', 7, 12))                 # 7
print(v.rindex(bytearray(b'pe'), 7, 12))      # 7
print(v.rindex(b'pe', -5, 12))                # 7
print(v.rindex(bytearray(b'pe'), -5, 12))     # 7

print(v.rindex(b''))                       # 12
print(v.rindex(bytearray(b'')))            # 12
print(v.rindex(b'g', 0, 1))                # 0
print(v.rindex(bytearray(b'g'), 0, 1))     # 0
print(v.rindex(ord('g'), 0, 1))            # 0
print(v.rindex(103, 0, 1))                 # 0
print(v.rindex(b'g', -12, -11))            # 0
print(v.rindex(bytearray(b'g'), -12, -11)) # 0
print(v.rindex(ord('g'), -12, -11))        # 0
print(v.rindex(103, -12, -11))             # 0
print(v.rindex(b'h', 11, 12))              # 11
print(v.rindex(bytearray(b'h'), 11, 12))   # 11
print(v.rindex(ord('h'), 11, 12))          # 11
print(v.rindex(104, 11, 12))               # 11
print(v.rindex(b'h', -1, 12))              # 11
print(v.rindex(bytearray(b'h'), -1, 12))   # 11
print(v.rindex(ord('h'), -1, 12))          # 11
print(v.rindex(104, -1, 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)