Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.count

bytearray — search

Count non-overlapping occurrences of a subsequence.

Syntax

ba.count(...)

Counting goes left to right and never overlaps, so b'aa' occurs twice in b'aaaa', not three times. Instead of a bytes literal you may pass an integer in 0-255, which means that single byte — a trick str.count() does not have. An empty subsequence returns the number of gaps between bytes, i.e. the length plus one.

Examples

print(bytearray(b'banana').count(b'a'))   # → 3
print(bytearray(b'aaaa').count(b'aa'))   # → 2
print(bytearray(b'banana').count(b'a', 2))   # → 2
print(bytearray(b'banana').count(97))   # → 3
print(bytearray(b'banana').count(b'z'))   # → 0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy