Summary: in this tutorial, you’ll learn how to use the Python string isnumeric()
method to check if all characters in a string are numeric characters.
Introduction to the Python string isnumeric() method
The string isnumeric()
method returns True
if:
- All characters in a string are numeric
- and the string contains at least one character.
The following shows the syntax of the isnumeric()
method:
str.isnumeric()
Code language: CSS (css)
Like isdigit()
method, the isnumeric()
method returns True
if all characters in the string are numeric characters 0-9.
In addition, it also returns True
if the string contains characters used in place of digits in other languages
For example, in Chinese, the number 1,2,3 is counted as 一,二,三. The isnumeric()
returns True
for these numeric characters:
result = '一二三'.isnumeric()
print(result)
Code language: PHP (php)
Output:
True
Code language: PHP (php)
However, the isdigit()
method returns False
in this case:
result = '一二三'.isdigit()
print(result)
Code language: PHP (php)
Output:
False
Code language: PHP (php)
This is the main difference between the isnumeric()
and isdigit()
methods.
Python string isnumeric() method example
The following example uses the isnumeric()
method to determine if all characters are numeric characters:
amount = '123'
result = amount.isnumeric()
print(result)
Code language: PHP (php)
Output:
True
Code language: PHP (php)
All the characters in the strings '99.99'
and '-10'
are not numeric, therefore, the isnumeric()
method returns False
:
price = '99.99'
print(price.isnumeric())
discount = '-10'
print(discount.isnumeric())
Code language: PHP (php)
Output:
False
False
Code language: PHP (php)
Summary
- Use the Python string
isnumeric()
to determine if all characters in a string are numeric characters.