Python String isnumeric () and its application

Counters | Python Methods and Functions | String Variables

Syntax :

  string  .isnumeric ()  Parameters:  isnumeric () does not take any parameters  Returns:  1.True- If all characters in the string are numeric characters. 2.False- If the string contains 1 or more non-numeric characters. 

Examples:

 Input: string = '1889345' Output: True Input: string =' u00BD' Output: True Input: string = '123ayu456' Output: False 

# Python code to implement isnumeric ()

  
# checking numeric characters

string = '123ayu456'

print (string.isnumeric ())

 

string = ' 123456'

print (string.isnumeric ())

Output:

 False True 

Errors and exclusions validation

  1. It does not contain any arguments, so it returns an error if a parameter is passed.
  2. Spaces are not considered numeric, so "False" is returned
  3. Subscript, superscript, fractions, roman numerals (all written in Unicode) are considered numeric, so it returns "True"

Usage: Having received string in python, count the number of numeric characters in the string, remove them from the string and print the string. 
Examples:

 Input: string = '123geeks456for789geeks' Output: 9 pythonengineering Input: string =' 123ayu456' Output: 6 aye 

Algorithm
1. Initialize the empty string newstring and the count variable to 0.
1. Go through the given string character by character to its length, check if the character is a numeric character. 
2. If it is a numeric character, increment the counter by 1 and do not add it to the new line, otherwise skip to the next character and keep adding characters to the new line if they are not numeric. 
3. Print the count and newline value.

Output:

 9 pythonengineering 6 ayu 




# Python implementation for counting numeric characters
# on the line and print non-numeric characters
# The given string
# Counter initialization to 0

string = '123geeks456for789geeks'

count = 0

 

newstring1 = ""

newstring2 = ""

 
# Iterating a string and checking for numeric characters
# Incrementing the counter if a numeric character is found
# And adding a character on a new line, if not numeric
# Finally print invoice and newline

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)