Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.rindex

bytes — search

Like rfind, but raises ValueError if not found.

Syntax

b.rindex(...)

Reach for rindex when a missing separator means the data is broken: rfind returns -1 instead, and -1 quietly works as a valid negative index in a slice, producing a plausible but wrong result. The number it returns is counted from the left — only the search direction is right-to-left. The argument must be a bytes-like object or an int in range 0-255; a str raises TypeError.

Examples

print(b'banana'.rindex(b'na'))   # → 4
print(b'banana'.rindex(b'a'))   # → 5
path = b'/usr/local/bin'
print(path[path.rindex(b'/') + 1:])   # → b'bin'
print(b'banana'.rindex(b'z'))   # → ValueError
print(b'banana'.rfind(b'z'))   # → -1

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy