bytes.count() scans a bytes object and returns an int: the number of non-overlapping places where sub occurs. It is handy for counting record separators in a packet, newline bytes in a file chunk, or padding bytes in a binary field.
The needle can be a bytes-like object such as b";" or, since Python 3.3, a single int from 0 to 255 such as 10 for a newline. Optional start and end limit the count to a slice, exactly like data[start:end].count(sub).
bytes.count() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.count(...). No import is needed, and bytearray objects have the same method.
Syntax
Pass the bytes (or byte value) to count, optionally with a slice range.
python
total = data.count(sub)
total = data.count(sub, start)
total = data.count(sub, start, end)
Call
What it does
b"a;b;c".count(b";")
Returns 2.
b"a\nb\n".count(10)
Returns 2: 10 is the newline byte.
b"aaaa".count(b"aa")
Returns 2, not 3: matches never overlap.
b"abc".count(b"")
Returns 4: an empty needle matches at every position, including the end.
count() never raises for a missing needle; it simply returns 0.
Method signatures
python
bytes.count(sub[, start[, end]])
# sub: bytes-like object or int in range(256)
# bytearray.count() works the same way
Parameters
Parameter
Description
sub
The byte sequence to count (bytes, bytearray, memoryview) or one byte value as an int 0-255.
start
Optional. Index where counting begins. Negative values count from the end.
end
Optional. Index where counting stops (exclusive).
Return Value
Return Value
Description
int
The number of non-overlapping occurrences of sub in data[start:end]; 0 when there are none.
Python Examples
Example 1 — Count Fields in a Sensor Packet
A tiny text-style packet uses ; to end each field and = to split names from values. Counting them is a quick sanity check before parsing.
Every zero byte. Five of the six bytes are \x00; only \xff at index 4 is different.
No overlapping pairs. The pair \x00\x00 is found at indexes 0 and 2; after a match, the scan jumps past it, and the lone zero at index 5 cannot form a pair.
Start offset. From index 4 the remaining bytes are \xff\x00, holding one zero.
Int with a range.count(0, 0, 3) looks only at the first three bytes, which are all zero.
Empty needle.b"" matches before each byte and at the end: 6 + 1 = 7.
Frequently Asked Questions
No. After each match the search continues after it, so b"aaaa".count(b"aa") is 2. Use a loop with find() and a step of 1 if you need overlapping matches.
Yes. Since Python 3.3 you can pass an int from 0 to 255, for example data.count(10) counts newline bytes.
Bytes methods only accept bytes-like arguments. Write data.count(b";") or encode the text first.
data.count(b"\n") counts newline bytes. If the last line has no newline, add one, or use len(data.splitlines()).
No. b";" in data only answers yes or no, while count() returns how many times it appears. For a yes or no answer, in is clearer and can stop at the first match.