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:
string.isnumeric()
If all the given characters in the string are numeric, the isnumeric
method will return True
.
If any one of the characters in the given string is not numeric, the isnumeric
method will return False
.
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, 123
is used as a string. All the characters are numeric in the string, so the output that will be displayed is True
.
In the second one, 2\u00B2
is used as a string that is equivalent to 2²
(superscript is also a numeric character). All the characters are numeric in the string, so the output that will be displayed is True
.
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 is True
.
In the fourth one, 12a
is used as a string, where a
in the string is not numeric, so False
will be returned.