Python String isnumeric() Function

Python String isnumeric() function returns True if all the characters in the input string are found to be of numeric type, else it returns False.

A numeric character can be of the following type:

  • numeric_character=Decimal
  • numeric_character=Digit
  • numeric_character=Numeric

Syntax:

input_string.isnumeric()

isnumeric() arguments: The isnumeric() function dosen’t take any argument as input.


Python isnumeric() Examples

Example 1:

string = '124678953'

print(string.isnumeric())

Output:

True

Example 2:

string = 'Divas Dwivedi . 124678953'

print(string.isnumeric())

Output:

False

Example 3: Special Unicode characters as the input string

string1 = '\u00B23455'
print(string1)
print(string1.isnumeric())

Output:

²3455
True

Example 4:

string = "000000000001" 
if string.isnumeric() == True:
    print("Numeric input")
else:  
    print("Not numeric")  
str = "000-9999-0110" 
if str.isnumeric() == True:
    print("Numeric input")
else:  
    print("Non numeric input")

Output:

Numeric input
Non numeric input

Access all Unicode numeric characters

The unicodedata module is used to fetch all the numeric Unicode characters.

import unicodedata

count_numeric = 0
for x in range(2 ** 16):
    str = chr(x)
    if str.isnumeric():
        print(u'{:04x}: {} ({})'.format(x, str, unicodedata.name(str, 'UNNAMED')))
        count_numeric = count_numeric + 1
print(f'Count of Numeric Unicode Characters = {count_numeric}')

Output:

0030: 0 (DIGIT ZERO)
0031: 1 (DIGIT ONE)
0032: 2 (DIGIT TWO)
.....
ff15: 5 (FULLWIDTH DIGIT FIVE)
ff16: 6 (FULLWIDTH DIGIT SIX)
ff17: 7 (FULLWIDTH DIGIT SEVEN)
ff18: 8 (FULLWIDTH DIGIT EIGHT)
ff19: 9 (FULLWIDTH DIGIT NINE)
Count of Numeric Unicode Characters = 800

Conclusion

Thus, in this article, we have studied and implemented the isnumeric() function of Python String.


References