The isnumeric( ) Function is used to check if all the characters of the String are numeric.
Say, there is a situation, where you have a number. But you need to store as a String.
In such cases, you can use isnumeric( ) Function to check if all the characters are numbers or not.
x = "249393854" if x.isnumeric(): print("All the characters in the String are numbers") else: print("No not all the characters in the String are numbers")
In the above code, we have declared a String '249393854' which has only numbers and assigned it to a variable 'x'.
Now, to check, if all are numbers in the above String. We have used the isnumeric() Function.
if x.isnumeric(): print("All the characters in the String are numbers")
Now since, all are numbers in the above String. The below output is printed.
Let us look at the second example, where the String has numbers and alphabets/letters.
x = "24A" if x.isnumeric(): print("All the characters in the String are numbers") else: print("No not all the characters in the String are numbers")
In the above code, we have declared a String '24A' which is a combination of numbers as well as alphabet.
Now, to check, if all are numbers in the above String. We have used the isnumeric( ) Function.
if x.isnumeric(): print("All the characters in the String are numbers") else: print("No not all the characters in the String are numbers")
And since, not all the elements are numbers, the control comes to the 'else' part printing the below output.