The isnumeric method
The isnumeric method checks whether a string contains only numbers. Unlike the isdigit method, the isnumeric method checks whether a string contains all types of numeric values, including Roman numerals and fractions.
Nothing is passed to the method parameters. The method returns the Boolean values True or False.
Syntax
string.isnumeric()
Example
Let's check that the string consists only of numbers:
txt = '12345'
print(txt.isnumeric())
Result of code execution:
True
Example
Now let's say there are other symbols in the line:
txt = '12345ab'
print(txt.isdigit())
Result of code execution:
False
Example
Now let's check a string containing Roman numerals using two methods isnumeric and isdigit:
txt = 'Ⅻ'
print('isdigit:', txt.isdigit())
print('isnumeric:', txt.isnumeric())
Result of code execution:
'isdigit:' False
'isnumeric:' True
Example
Let's check if a string contains a fractional number using the isnumeric and isdigit methods:
txt = '⅓'
print('isdigit:', txt.isdigit())
print('isnumeric:', txt.isnumeric())
Result of code execution:
'isdigit:' False
'isnumeric:' True