Skip to content
Related Articles

Related Articles

Improve Article

Python String isnumeric() Method

  • Last Updated : 12 Aug, 2021
Geek Week

Python String isnumeric() method is a built-in method used for string handling. The issnumeric() method returns “True” if all characters in the string are numeric characters, Otherwise, It returns “False”. 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)

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.

Errors and Exceptions:

  1. It does not contain any arguments, therefore, it returns an error if a parameter is passed.
  2. Whitespaces are not considered to be numeric, therefore, it returns “False”
  3. Subscript, Superscript, Fractions, Roman numerals (all written in Unicode)are all considered to be numeric, Therefore, it returns “True”

Example 1:

Input : string = '1889345'
Output : True

Input : string = '\u00BD'
Output : True

Input : string = '123ayu456'
Output : False

Python3




# Python code for implementation of isnumeric()
    
# checking for numeric characters
string = '123ayu456'
print(string.isnumeric())
   
string = '123456'
print( string.isnumeric())

Output: 

False
True

Example 2:

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. 

Input : string = '123geeks456for789geeks'
Output : 9
         geeksforgeeks

Input : string = '123ayu456'
Output : 6
         ayu

Algorithm:

  1. Initialize an empty NewString 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 value of the counter and the NewString.

Python3




# Python implementation to count numeric characters
# in a string and print non numeric characters
# Given string
# Initialising the counter to 0
string ='123geeks456for789geeks'
count = 0
  
newstring1 =""
newstring2 =""
    
# Iterating the string and checking for numeric characters
# Incrementing the counter if a numeric character is found
# And adding the character to new string if not numeric
# Finally printing the count and the newstring
for a in string:
    if (a.isnumeric()) == True:
        count+= 1
    else:
        newstring1+= a
print(count)
print(newstring1)
   
string ='123ayu456'
count = 0
for a in string:
    if (a.isnumeric()) == True:
        count+= 1
    else:
        newstring2+= a
print(count)
print(newstring2)

Output: 

9
geeksforgeeks
6
ayu

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :