Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Python String isnumeric()

Python String isnumeric() function is used to check if all the characters of a string are of numeric type or not.

  • Python String isnumeric() is an inbuilt function.

  • The isnumeric() method returns true if all the characters of a string are of numeric type otherwise it will return false.

  • There are different types of numerics and these are as follows:

    • integers,

    • decimals,

    • subscripts,

    • roman numerals,

    • superscripts, etc.

  • There is one special thing about numerics and that is they all can be represented in Unicode.

  • Another advantage of isnumeric() method is that it is useful in counting the number of numerics in a string.

Python String isnumeric(): Syntax

Below we have the basic syntax of the string isnumeric() method in Python:

string.isnumeric() 

Note: In the above syntax, the string is used to denote the value of the string variable that is to be checked.

Also, it is clear from the above syntax, that this method does not require any parameters.

Python String isnumeric(): Returned Values

For the returned value there are two cases:

  • If the string contains all numeral characters then this method returns true.

  • If the string contains more than one non-numeric value(alphabet or any special character) then this method returns false.

Python String isnumeric(): Basic Example

Below we have an example to show the working of String isnumeric() function:

str1 = "12345678"
str2="abcd123k"
str3="\u00BD"
print("String: ", str1, " Numeric: ", str1.isnumeric())
print("String: ", str2, " Numeric: ", str2.isnumeric())
print("String: ", str3, " Numeric: ", str3.isnumeric())

The Output for the same is given below:


String: 12345678 Numeric: True
String: abcd123k Numeric: False
String: ½ Numeric: True

Time for a Live Example!

Let us see a Live Example given below where we will use isnumeric() function under different conditions:

Summary

In this tutorial, we studied isnumeric() method which returns true in the case if string consists of only numeric values, else it returns false. We saw its basic syntax with a few examples.

This function can be used to validate user input value, if any field is supposed to have only numeric values then we can use this function to check if user has entered a numeric value or not.