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 propertieslike fractions, roman numerals, and currency numerators

Syntax

string.isnumeric()

Return value

  • 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.

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, 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 (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.

Free Resources