Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.rfind

bytearray — search

Index of the last occurrence, or -1.

Syntax

ba.rfind(...)

The scan runs right to left, yet the index it returns is still measured from the start, so rfind and find report different numbers for the very same needle. start and end define the ordinary [start, end) window; nothing is reversed there — the direction only decides which match inside that window wins.

Examples

print(bytearray(b'banana').rfind(b'na'))   # → 4
print(bytearray(b'banana').find(b'na'))   # → 2
print(bytearray(b'banana').rfind(b'na', 0, 4))   # → 2
print(bytearray(b'banana').rfind(b'z'))   # → -1
print(bytearray(b'banana').rfind(97))   # → 5
print(bytearray(b'banana').rindex(b'z'))   # → ValueError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy