Bytearray Methods

bytearray.count()

Count non-overlapping occurrences of the binary subsequence. Searches accept a bytes-like object or a single integer byte (0–255). Bounds restrict complete matches; offsets are relative to the original sequence. Returns zero when absent. Empty patterns match boundaries, but a start greater than the original length does not match. Positions count bytes, not decoded characters. Integer search support was added in Python 3.3.

Formal Syntax & Structure

python syntaxpython
data.count(sub[, start[, end]])

Parameters & Attributes

NameTypeDescription
subbytes-like | intBinary subsequence or single byte value from 0 through 255.
startint | NoneInclusive search start; negative values count from the end.
endint | NoneExclusive search end; defaults to the end of the data.

Return Value

int

Number of non-overlapping matches.

Working Example

Example Snippetpython
print(bytearray(b'AB:AB:Z').count(b'AB'))print(bytearray(b'AB:AB:Z').count(58))print(bytearray(b'AB:AB:Z').count(memoryview(b'AB'), 1, 6))
Python Reference