Python String isnumeric() Method
The isnumeric()
method checks whether all characters in a string are numeric characters.
Syntax
string.isnumeric()
Parameters
No parameters.
Return Value
Returns True
if all characters in a string are numeric.
Returns False
if the string is empty or contains any non-numeric characters.
What Counts as Numeric Characters?
The numeric()
method considers the following as numeric:
- Digits (0-9)
- Superscripts (¹, ²)
- Subscripts (₁, ₂)
- Fractions (½, ¾)
- Characters from other number systems (like Roman numerals, Chinese/Arabic/Japanese/Devanagari numerals)
Examples
Check if a String is Numeric
print("123".isnumeric()) # True
print("\u0035".isnumeric()) # True (Unicode for 5)
print("²".isnumeric()) # True (Superscript digit)
print("₃".isnumeric()) # True (Subscript digit)
print('٢'.isnumeric()) # True (2 in Arabic)
print("1.5".isnumeric()) # False (contains decimal point)
print("123abc".isnumeric()) # False (contains letters)
print("-5".isnumeric()) # False (contains negative sign)
print("½".isnumeric()) # True (Fraction: one-half)
isnumeric()
vs isdigit()
method
Both isnumeric()
and isdigit()
are used to check if characters in a string are numeric, but they have important differences in what they consider “numeric”.
Character Type | isnumeric() | isdigit() |
---|---|---|
Basic Digits (0–9) | True | True |
Arabic Digits (e.g., ٢) | True | True |
Devanagari Digits (e.g., १२) | True | True |
Superscripts (², ³) | True | True |
Subscripts (₁, ₂) | True | True |
Fractions (½, ¾) | True | False |
Roman Numerals (Ⅷ, Ⅻ) | True | False |
Floating Point Numbers (1.5) | False | False |
Negative Numbers (-5) | False | False |
Empty String (“”) | False | False |