Python bytes.isdigit() Method

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

Definition

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()
CallWhat 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 ValueDescription
TrueAt least one byte, and all are ASCII digits.
FalseEmpty, 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())

How It Works

  1. Only 0-9. The year is made of ASCII digits and passes.
  2. Sign and point. - and . are not digits.
  3. 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")

How It Works

  1. Extract the value. partition() returns the part after ": ".
  2. Guard the conversion. int() runs only when the check passes, so bad input never raises ValueError.
  3. 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.

python
three = "\u0663"
square = "\u00b2"

print(three.isdigit(), three.encode("utf-8").isdigit())
print(square.isdigit(), square.encode("utf-8").isdigit())
print(int(three))

How It Works

  1. Arabic-Indic three. str.isdigit() accepts it; its UTF-8 bytes do not pass.
  2. Superscript two. The str check counts it as a digit too, while the bytes check does not.
  3. int() understands it. int() converts the Arabic-Indic digit to 3, one reason the bytes check is stricter and more predictable.

Frequently Asked Questions

Only ASCII 0-9 (values 48 to 57).
The minus sign is not a digit. Check data.lstrip(b"-").isdigit() or use try/except around int().
No. Those methods exist only on str.
Yes. int(b"512") returns 512 for ASCII digits.
The method requires at least one byte.

Next: bytes.islower()

True if there is at least one ASCII lowercase letter and no ASCII uppercase letters.

islower() →

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.

11 people found this page helpful