Python bytes.count() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
bytes method

Definition

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)
CallWhat 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

ParameterDescription
subThe byte sequence to count (bytes, bytearray, memoryview) or one byte value as an int 0-255.
startOptional. Index where counting begins. Negative values count from the end.
endOptional. Index where counting stops (exclusive).

Return Value

Return ValueDescription
intThe 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.

python
packet = b"ID=7;TEMP=21;HUM=40;"

print(packet.count(b";"))
print(packet.count(b"="))
print(packet.count(b"TEMP"))
print(packet.count(b"X"))

How It Works

  1. Field terminators. Each of the three fields ends with ;, so the first count is 3.
  2. Balanced packet. Three = signs match the three terminators, which suggests no field is broken.
  3. Multi-byte needle. b"TEMP" is counted as one four-byte sequence and appears once.
  4. Missing is zero. b"X" never appears, and count() returns 0 instead of raising.

Example 2 — Count Newline Bytes with an int Needle

Read-style binary chunks often need a line count. Pass the newline as b"\n" or as its byte value 10; ord() turns any ASCII character into that int.

python
chunk = b"line one\nline two\nline three\n"

print(chunk.count(b"\n"))
print(chunk.count(10))
print(chunk.count(ord("e")))
print(len(chunk.splitlines()))

How It Works

  1. Bytes needle. b"\n" matches the three line endings.
  2. Same answer with an int. 10 is the value of the newline byte, so count(10) also returns 3.
  3. ord() helper. ord("e") is 101; the letter e appears twice in line one, once in line two, and three times in line three.
  4. Cross-check. splitlines() produces three lines, agreeing with the newline count because the chunk ends with a newline.

Example 3 — Count Zero Bytes with Ranges and Overlaps

Binary records are often padded with zero bytes. This example counts them in different ways and shows two rules that surprise beginners.

python
data = b"\x00\x00\x00\x00\xff\x00"

print(data.count(b"\x00"))
print(data.count(b"\x00\x00"))
print(data.count(b"\x00", 4))
print(data.count(0, 0, 3))
print(data.count(b""))

How It Works

  1. Every zero byte. Five of the six bytes are \x00; only \xff at index 4 is different.
  2. 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.
  3. Start offset. From index 4 the remaining bytes are \xff\x00, holding one zero.
  4. Int with a range. count(0, 0, 3) looks only at the first three bytes, which are all zero.
  5. 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.

Next: bytes.decode()

Decode bytes into a str using an encoding such as UTF-8.

decode() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful