Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Python String isnumeric() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

//add other examples of isnumeric() Method

Python String isnumeric() method returns “True” if all characters in the string are numeric characters, otherwise returns “False”. 

Python String isnumeric() Method Syntax

Syntax:  string.isnumeric()

Parameters: isnumeric() does not take any parameters

Returns :

  • True – If all characters in the string are numeric characters.
  • False – If the string contains 1 or more non-numeric characters.

Python String isnumeric() Method Example

Python3




print("1299".isnumeric())

Output:

True

This function is used to check if the argument contains all numeric characters such as integers, fractions, subscript, superscript, Roman numerals, etc.(all written in Unicode).

Example 1: Basic Examples using Python String isnumeric() Method

Python3




string = '123ayu456'
print(string.isnumeric())
  
string = '123456'
print( string.isnumeric())

Output: 

False
True

Example 2: Checking for numeric characters using isnumeric() function

Application: Given a string in Python, count the number of numeric characters in the string and remove them from the string, and print the string. 

Algorithm:

  1. Initialize an empty new_string and variable count to 0. 
  2. Traverse the given string character by character up to its length, check if the character is a numeric character. 
  3. If it is a numeric character, increment the counter by 1 and do not add it to the new string, else traverse to the next character and keep adding the characters to the new string if not numeric. 
  4. Print the count of numeric characters and the new string.

Python3




# Given string
string ='123geeks456for789geeks'
count = 0
new_string =""
   
for ch in string:
    if ch.isnumeric():
        count += 1
    else:
        new_string += ch
 
print("Number of numeric characters:", count)
print("String after removing numeric characters:", new_string)

Output: 

Number of numeric characters: 9
String after removing numeric characters: geeksforgeeks

Errors and Exceptions:

  • It does not contain any arguments, therefore, it returns an error if a parameter is passed.

Python3




# isnumeric() returns an error if a parameter is passed
String = "1234567"
 
try:
    String.isnumeric("abc")
except TypeError:
    print("TypeError: isnumeric() takes no arguments (1 given)")

Output

TypeError: isnumeric() takes no arguments (1 given)
  • White-spaces are not considered to be numeric, therefore, it returns “False”.

Python3




# isnumeric() to check White-spaces
s = " "
p = "12 3"
 
print(s.isnumeric())  # False
print(p.isnumeric())  # False
# This code is contributed by Susobhan Akhuli

Output

False
False
  • Subscript, Superscript, Fractions, Roman numerals (all written in Unicode)are all considered to be numeric, Therefore, it returns “True”.

Python3




string1 = '123'
string2 = '⅓'
string3 = '²'
string4 = '2167'  # 'Ⅷ'; ROMAN NUMERAL EIGHT
 
print(string1.isnumeric())  # True
print(string2.isnumeric())  # True
print(string3.isnumeric())  # True
print(string4.isnumeric())  # True
 
# This code is contributed by Susobhan Akhuli

Output

True
True
True
True

My Personal Notes arrow_drop_up
Last Updated : 16 Mar, 2023
Like Article
Save Article
Similar Reads
Related Tutorials