Python Reference Β· String Methods
Python str.isnumeric() method
Reference for Python str.isnumeric(): check for numeric characters including fractions, Roman numerals and CJK numbers, and learn why it does not mean int() works.
The isnumeric() method returns True if the string is not empty and every character has a Unicode numeric value. That is the widest of the three digit tests: it accepts ordinary digits, superscripts, fractions such as Β½, Roman numeral characters such as β
£, and numbers written in other scripts such as δΊ.
Having a numeric value does not make a string parseable. Use it to classify characters, not to promise that int() or float() will succeed.
print("12345".isnumeric()) # True print("Β½".isnumeric()) # True
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input Β· what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
str.isnumeric()
This method takes no parameters.
Return value: True if the string has at least one character and all characters have the Unicode property Numeric_Type of Decimal, Digit or Numeric; otherwise False.
Raises: nothing.
Usage notes
- Superset of the others. Every string that passes
isdecimal()passesisdigit(), and every string that passesisdigit()passesisnumeric(). The reverse is not true. - No signs or points.
"-1"and"3.5"areFalsebecause-and.have no numeric value. To validate numbers, callfloat()orint()in atry/except ValueErrorblock. - Not an
int()test."Β½".isnumeric()isTrue, butint("Β½")raisesValueError. - Empty string is
False. - Looking up the value.
unicodedata.numeric(char)returns the numeric value of a single character as a float, for example0.5for"Β½". - Scripts. Characters like
δΊorβ «count, which can be a surprise in forms that expect Western digits. Addisascii()when you need0-9only. - Bytes have no
isnumeric(); usebytes.isdigit().