Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.endswith

bytearray — search

Check whether the bytearray ends with the given suffix; a tuple of suffixes is accepted too.

Syntax

ba.endswith(...)

The comparison is byte-for-byte and case-sensitive, so b'.TXT' will not match b'.txt' — normalise with lower() first. The argument must be a bytes-like object or a tuple of them: a str raises TypeError, and unlike count() or find(), a bare integer is not accepted at all.

Examples

print(bytearray(b'hello').endswith(b'lo'))   # → True
print(bytearray(b'file.txt').endswith((b'.txt', b'.md')))   # → True
print(bytearray(b'hello').endswith(b'll', 0, 4))   # → True
print(bytearray(b'log.txt').endswith(b'.TXT'))   # → False
print(bytearray(b'hello').endswith('lo'))   # → TypeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy