Determines whether the string contains numeric characters
Usage
The isnumeric()
method returns TRUE if the string is nonempty and all characters in it are numeric characters. Otherwise, it returns FALSE.
Numeric characters include digit characters, and all characters that have the Unicode numeric value property.
e.g. ² (U+00b2, Superscript Two), ⅕ (U+2155, Vulgar Fraction One Fifth)
Syntax
string.isnumeric()
Basic Examples
# Check if all characters in the string are numeric characters
S = '123'
x = S.isnumeric()
print(x)
# Prints True
Below are a few examples where isnumeric()
method returns false.
# floating point number
S = '123.456'
x = S.isnumeric()
print(x)
# Prints False
# number with thousands separator
S = '1,234,567'
x = S.isnumeric()
print(x)
# Prints False
# empty string
S = ''
x = S.isnumeric()
print(x)
# Prints False
isnumeric() on Unicode Numeric Characters
Unicode character such as superscript digit ²
is considered as a numeric character.
S = '\u00b2'
x = S.isnumeric()
print(x)
# Prints True
Unicode character like Vulgar Fraction One Third ⅓
is also considered as a numeric.
S = '\u2153'
x = S.isnumeric()
print(x)
# Prints True
isdecimal() vs isdigit() vs isnumeric()
Following examples explain the difference between the three methods.
# Is 42 a decimal or digit or numeric number?
print('42'.isdecimal()) # Prints True
print('42'.isdigit()) # Prints True
print('42'.isnumeric()) # Prints True
# Is ² (Superscript Two) a decimal or digit or numeric number?
print('\u00b2'.isdecimal()) # Prints False
print('\u00b2'.isdigit()) # Prints True
print('\u00b2'.isnumeric()) # Prints True
# Is ⅓ (Vulgar Fraction One Third) a decimal or digit or numeric number?
print('\u2153'.isdecimal()) # Prints False
print('\u2153'.isdigit()) # Prints False
print('\u2153'.isnumeric()) # Prints True
As you can see, the main difference between the three functions is:
- isdecimal() method supports only Decimal Numbers.
- isdigit() method supports Decimals, Subscripts, Superscripts.
- isnumeric() method supports Digits, Vulgar Fractions, Subscripts, Superscripts, Roman Numerals, Currency Numerators.