bytes.isdigit() returns True if the object is not empty and every byte is one of the ASCII digits 0-9 (values 48-57).
A minus sign, a decimal point, spaces, and every non-ASCII byte make it False. That makes it a strict check for unsigned integer fields in text protocols before calling int().
bytes.isdigit() is a method of the built-in bytes type: call it on a bytes object with dot notation, such as data.isdigit(...). No import is needed, and bytearray objects have the same method.
Syntax
No arguments; returns a bool.
python
result = data.isdigit()
Call
What it does
b"2026".isdigit()
Returns True.
b"-5".isdigit()
Returns False: the sign is not a digit.
b"3.14".isdigit()
Returns False.
bytes has no isdecimal() or isnumeric(); those exist only on str.
Method signatures
python
bytes.isdigit()
# returns True or False; bytearray.isdigit() works the same way
Parameters
isdigit() takes no parameters.
Return Value
Return Value
Description
True
At least one byte, and all are ASCII digits.
False
Empty, or contains any other byte.
Python Examples
Example 1 — Digits, Signs, and Decimals
Check a year, a negative number, a decimal, empty bytes, and a UTF-8 encoded Arabic-Indic digit.
python
for s in [b"2026", b"-5", b"3.14", b"", b"\xd9\xa3"]:
print(s, s.isdigit())
Only 0-9. The year is made of ASCII digits and passes.
Sign and point.- and . are not digits.
Non-ASCII digit. The two bytes encode the Arabic-Indic digit three, which bytes do not treat as a digit.
Example 2 — Validate a Content-Length Header
Only convert the header value to an int when it is purely digits; otherwise report it as invalid.
python
headers = [b"Content-Length: 512", b"Content-Length: 12kb", b"Content-Length: "]
for h in headers:
value = h.partition(b": ")[2]
print(int(value) if value.isdigit() else "invalid")
Output
512
invalid
invalid
How It Works
Extract the value.partition() returns the part after ": ".
Guard the conversion.int() runs only when the check passes, so bad input never raises ValueError.
Units and empty values.12kb contains letters, and an empty value fails because there must be at least one byte.
Example 3 — bytes.isdigit() vs str.isdigit()
Two Unicode digit characters pass the str check but fail as UTF-8 bytes.