all Python 3Byte sequencesfunction
bytearray.rindex
bytearray — search
Like rfind, but raises ValueError if not found.
Syntax
ba.rindex(...)
Use it where the last occurrence is required by contract: a separator in a path, the dot before an extension. A vanished separator then raises instead of silently sliding to -1 and producing a nonsense slice. For the common "everything after the last separator" job, rpartition() is usually cleaner — it handles the not-found case for you, no try/except needed.
Examples
print(bytearray(b'banana').rindex(b'na')) # → 4
print(bytearray(b'banana').rindex(b'a')) # → 5
print(bytearray(b'banana').rindex(b'na', 0, 4)) # → 2
print(bytearray(b'banana').rindex(b'z')) # → ValueError
print(bytearray(b'banana').rfind(b'z')) # → -1
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy