Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.rfind

bytes — search

Return the index of the last occurrence, or -1.

Syntax

b.rfind(...)

The scan runs right to left, but the returned index is still counted from the start of the object, so it plugs straight into a slice. The start and end arguments still define a left-to-right window, and you get the rightmost match that fits entirely inside it. For the usual job of cutting at the last separator, rpartition or rsplit is cleaner than doing index arithmetic by hand.

Examples

print(b'banana'.rfind(b'na'))   # → 4
print(b'banana'.find(b'na'))   # → 2
print(b'banana'.rfind(b'na', 0, 4))   # → 2
name = b'archive.tar.gz'
print(name[name.rfind(b'.') + 1:])   # → b'gz'
print(b'banana'.rfind(b'z'))   # → -1

See also

Open in the full glossarydocs.python.orgReport an inaccuracy