String method

str.isnumeric()

Returns True when every character is a number of some kind: digits, superscripts, fractions like ½ or Roman numerals.

IntermediateEvery example below runs in your browser. Change the code and press Run.

str.isnumeric()

isnumeric() is the widest of Python's three number checks. It returns True for a non-empty string whose characters all have a numeric meaning: ordinary digits, digits from other scripts, superscripts like "²", fractions like "½", Roman numerals like "Ⅻ" and Chinese numerals like "五".

That width is the catch: isnumeric() returning True does not mean int() or float() can read the text. int("½") still fails.

From narrow to wide: isdecimal() (only 0–9 in any script, safe for int()), then str.isdigit() (adds superscripts), then isnumeric() (adds fractions and numerals). Like the others, it rejects "-", "." and spaces.

Return value

True or False.

Examples

1Things isnumeric() accepts

Pythoneditable
for s in ["123", "²", "½", "Ⅻ", "五"]:
    print(s, s.isnumeric())
Output
123 True
² True
½ True
Ⅻ True
五 True

2Things it rejects

Pythoneditable
for s in ["-5", "2.5", "1 000", ""]:
    print(repr(s), s.isnumeric())
Output
'-5' False
'2.5' False
'1 000' False
'' False

3Compare the three checks

Pythoneditable
samples = ["42", "²", "½", "٣", "Ⅻ"]
print("text  decimal digit numeric")
for s in samples:
    print(f"{s:<5} {s.isdecimal()!s:<7} {s.isdigit()!s:<5} {s.isnumeric()}")
Output
text  decimal digit numeric
42    True    True  True
²     False   True  True
½     False   False True
٣     True    True  True
Ⅻ     False   False True

4Get the value of a numeric character

Pythoneditable
import unicodedata
for s in ["½", "Ⅻ", "五"]:
    print(s, unicodedata.numeric(s))
Output
½ 0.5
Ⅻ 12.0
五 5.0

unicodedata.numeric() reads the value of a single numeric character, which int() and float() cannot.

Common mistakes

Using isnumeric() before int()

A True from isnumeric() does not make the text safe for int(). Use isdecimal() for that.

Pythoneditable
text = "½"
print(text.isnumeric())
try:
    int(text)
except ValueError:
    print("int() failed")
print(text.isdecimal())
Output
True
int() failed
False

Questions people ask

What is the difference between isnumeric() and isdigit()?

isnumeric() also accepts fractions such as ½, Roman numerals and Chinese numerals; str.isdigit() does not.

Which check should I use to validate a number typed by a user?

isdecimal() for whole numbers, or simply try: int(text) / float(text) with except ValueError.

Does isnumeric() exist for bytes?

No. Byte strings have isdigit() but not isnumeric() or isdecimal().

Spotted a mistake on this page? Tell us