Python Glossary Русский

all Python 3Byte sequencesfunction

bytes.isdigit

bytes — is* checks

Check that all bytes are ASCII digits and non-empty.

Syntax

b.isdigit(...)

The bytes type has no isdecimal() or isnumeric() — only isdigit(), and it accepts exactly the bytes b'0' through b'9', with none of the superscripts or other Unicode digits that str.isdigit() would let through. Treat it as a check for a field of pure digits, not for "looks like a number": a sign, a dot or surrounding spaces all give False, even though int() would happily parse such bytes (int(b' -7 ') works).

Examples

print(b'123'.isdigit())   # → True
print(b'12.5'.isdigit())   # → False
print(b'-7'.isdigit())   # → False
raw = b'42'
print(int(raw) if raw.isdigit() else 0)   # → 42
print('²'.isdigit(), b'\xb2'.isdigit())   # → True False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy