What is string.isnumeric() in Python?
The isnumeric() method is a string function in Python that is used to find whether or not all the characters given in a string are numeric. The numeric characters include:
- The numbers 0-9
- Subscript & superscript
Unicode numeric value properties like fractions, roman numerals, and currency numerators
Syntax
string.isnumeric()
Return value
-
If all the given characters in the string are numeric, the
isnumericmethod will returnTrue. -
If any one of the characters in the given string is not numeric, the
isnumericmethod will returnFalse.
Example
num = '123'print( num , 'isnumeric -- ', num.isnumeric())num = '2\u00B2' #equivalent for 2²print( num , 'isnumeric -- ', num.isnumeric())num = '½'print( num , 'isnumeric -- ', num.isnumeric())num='12a'print( num , 'isnumeric -- ', num.isnumeric())
-
In the first one,
123is used as a string. All the characters are numeric in the string, so the output that will be displayed isTrue. -
In the second one,
2\u00B2is used as a string that is equivalent to2²(superscript is also a numeric character). All the characters are numeric in the string, so the output that will be displayed isTrue. -
In the third one,
½is used as a string that is a fractional number (the fractional number is also a numeric character). All the characters are numeric in the string, so the output that will be displayed isTrue. -
In the fourth one,
12ais used as a string, whereain the string is not numeric, soFalsewill be returned.