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

pythonstring.isnumeric()

Example

Let's check that the string consists only of numbers:

pythontxt = '12345'
print(txt.isnumeric())

Result of code execution:

pythonTrue

Example

Now let's say there are other symbols in the line:

pythontxt = '12345ab'
print(txt.isdigit())

Result of code execution:

pythonFalse

Example

Now let's check a string containing Roman numerals using two methods isnumeric and isdigit:

pythontxt = 'Ⅻ'
print('isdigit:', txt.isdigit())
print('isnumeric:', txt.isnumeric())

Result of code execution:

python'isdigit:' False
'isnumeric:' True

Example

Let's check if a string contains a fractional number using the isnumeric and isdigit methods:

pythontxt = '⅓'
print('isdigit:', txt.isdigit())
print('isnumeric:', txt.isnumeric())

Result of code execution:

python'isdigit:' False
'isnumeric:' True

See also

  • method isdigit,
    which checks if a string contains only numbers
  • method isalnum,
    which checks if a string contains letters and numbers
  • method isalpha,
    which checks if a string contains only letters
  • method isspace,
    which checks if a string contains only spaces