Python

Python Check If String is Number

Oftentimes, the developer of an application needs to check whether the input made by the end-user contains all numeric values or not. This is not a difficult task, but when the input is in the form of a string, checking whether the string contains all numeric values can be difficult.

Various methods can be used to check whether a string in python is a numeric string or not. The term numeric string will be used a lot in this post as it is used to show that a string only contains a numeric value.

Method 1: How to Check if String is Number Using the “isnumeric()” Method?

Python provides a built-in method “isnumeric()” to verify if a string is a numeric string or not. To use this method, the user has to call this method using a dot operator on the variable that contains the string in question. Take a look at its syntax to get a better understanding:

resultVar = stringVar.isnumeric()

 
The return value of this method is “boolean”, which means that it either returns true or false.

To demonstrate the use of this method, take the following code snippet:

stringVar = "123654"
resultVar = stringVar.isnumeric()
print(resultVar)

 
Upon executing this code, the following output is displayed on the terminal:


The output verifies that the string in question is indeed a numeric string. Let’s take another example in which the string contains some other characters as well:

stringVar = "168Hello11"
resultVar = stringVar.isnumeric()
print(resultVar)

 
Running this above code will produce the following result on the terminal:


The output confirms that this time the string is not a numeric string.

Method 2: How to Check if String is Number Using Type Conversion?

This method mainly deals with manual type casting and finding the conclusion depending upon error handling. When a numeric string is converted into an integer data type, no errors are generated. However, if a non-numeric string is converted into an integer, then the program runs into an error, and the program crashes. With the help of exception handling, the crash can be avoided, and the result can be easily generated.

To demonstrate this, take the following code snippet:

stringVar='9932'
isNumericString = True;

try:
    int(stringVar)
except ValueError:
    isNumericString=False

if isNumericString:
    print('It is a Numeric String')
else:
    print('It is not a Numeric String')

 
In the above code snippet:

    • Create a string variable and a boolean variable
    • Try converting the string variable into an integer using the int() method inside the “try” statement
    • If there is an error or exception in the conversion then change the value of the “isNumericString” to false.
    • Otherwise, don’t change the value
    • Lastly, depending on the value of the “isNumericString”, tell the result to the user using the print() function.

Upon executing this command, the following output is displayed on the terminal:


This proves that the string is indeed a numeric string.

Method 3: How to Check if String is Number Using the “isdigit()” Method?

Another built-in method provided by Python is the “isdigit()” method. This method also works the same as the “isnumeric()” method, but the only difference is that it can only check for positive integer values and not for floating point or negative integer values.

To demonstrate its use, take the following code snippet:

stringVar= "456"
print(stringVar.isdigit())

 
The output of this code is as follows:


This provides that the string contains a positive integer value. Alternatively, use the following code to check the output for a non-numeric string:

stringVar= "123Hello"
print(stringVar.isdigit())

 
This prints the following result onto the terminal:


From this output image above, it can be observed the string doesn’t contain a positive integer value.

Conclusion

Verifying that a string is a numeric string or number can easily be done in Python by either using the built-in methods “isnumeric()” and “isdigit()” or also through the exception-handling and type conversion method. The built-in methods have a boolean return type, which means that they will return “True” when the string is a numeric string, otherwise, false. This post has elaborated on the different methods that can be used to check if the string is a number in Python.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.