Skip to Content

Python – Check If Input Is a Number

In this tutorial, we will look at how to check if a user input in Python is a number or not with the help of some examples.

How to check if the user input is a number?

check if user input in python is a number

In Python, we receive the value from the standard input as strings. Now, to check if the input is a number or not, check if the input string is numeric or not.

There are multiple ways to check if a string is numeric (contains only numeric data) or not in Python. For example –

  • Try to convert the string into a float (or int) type inside a tryexcept block. If the code raises an error, we can say that the input string is not numeric.
  • Use the string built-in isdigit() function.
  • Use the string built-in isnumeric() function.

Examples

Let’s now look at some examples so using the above syntax. First, we will take some values as input and then use them throughout this tutorial.

# take input from the user
s1 = input("Enter the first value\n")
s2 = input("Enter the second value\n")
s3 = input("Enter the third value\n")
s4 = input("Enter the fourth value\n")

# display the entered values
for s in [s1, s2, s3, s4]:
    print(f"Entered value: {s} has type: {type(s)}")

Output:

Enter the first value
21
Enter the second value
3.14
Enter the third value
cat
Enter the fourth value

Entered value: 21 has type: <class 'str'>
Entered value: 3.14 has type: <class 'str'>
Entered value: cat has type: <class 'str'>
Entered value:  has type: <class 'str'>

Here, we take four values as input from the user – s1, s2, s3, and s4. All the values from the standard input are of string type. You can see that the string s1 is a numeric string, s2 is also a numeric string (with a decimal point), s3 contains alphabetical characters, and s4 is an empty string.

Method 1 – Using type conversion inside tryexcept

You can also use error handling to check if the string input is a number or not.

The idea is to try to convert the string input to a numeric type (like int or float) inside a try block. Now, if this conversion raises a ValueError, we can say that the input is not a number.

Let’s apply this method to the numeric string s1 created above.

# check if s1 is numeric or not
try:
    # convert string to float
    float(s1)
    print("Input is a number")
except ValueError:
    print("Type conversion failed, input is not a number")

Output:

Input is a number

Now, let’s apply this method to all the strings created above.

# check if input string is a number
for s in [s1, s2, s3, s4]:
    print("For ", s)
    try:
        # convert string to int
        float(s)
        print("Input is a number")
    except ValueError:
        print("Type conversion failed, input is not a number")

Output:

For  21
Input is a number
For  3.14
Input is a number
For  cat
Type conversion failed, input is not a number
For  
Type conversion failed, input is not a number

The error-handling method correctly identified the numeric strings (s1 and s2) in the above example.

Method 2 – Using the string isdigit() function

The string isdigit() function takes a string and returns True if all the characters in the string are numeric characters and there is at least one character in the string. It returns False otherwise.

Let’s now use this method on the four input strings we created above.

# check if input string is a number
print(s1.isdigit())
print(s2.isdigit())
print(s3.isdigit())
print(s4.isdigit())

Output:

True
False
False
False

We get True for s1 and False for s2, s3 and s4.

Notice that, here, we get False for s2 which is “3.14”. The reason we get False is because of the “.” character present in the number.

So, the isdigit() function doesn’t directly work for numbers with decimal values but there’s a workaround. You can first replace any '.' characters with an empty string and then proceed to use the isdigit() function.

# check if input string with decimal is a number
print(s2.replace(".", "").isdigit())

Output:

True

Now we get True for s2 (it correctly identifies it as a numeric value).

Method 3 – Using the string isnumeric() function

The string isnumeric() function, similar to the isdigit() function returns True if all the characters in a string are numeric characters and there’s at least one character in the string. Otherwise, it returns False.

Let’s now apply this function to the strings created above.

# check if input string is a number
print(s1.isnumeric())
print(s2.isnumeric())
print(s3.isnumeric())
print(s4.isnumeric())

Output:

True
False
False
False

We get True for s1 and False for s2, s3 and s4. Same as the above example.

Notice that, here, similar to the above example, we get False for s2 which is “3.14”. The reason we get False is because of the “.” character present in the number.

So, the isnumeric() function doesn’t directly work for numbers with decimal values but there’s a workaround. You can first replace any '.' characters with an empty string and then proceed to use the isnumeric() function.

# check if input string with decimal is a number
print(s2.replace(".", "").isnumeric())

Output:

True

Now, we get True for s2 (it correctly identifies it as a numeric value).

Summary

In this tutorial, we looked at multiple ways to check if the user input is a number or not. The following are the methods covered –

  1. Trying to convert the input to a numeric type (for example, float) inside a try block and catch any ValueErrors that may occur.
  2. Use the string isdigit() function. Remember to remove any '.' characters in the string to identify numeric strings with decimal values.
  3. Use the string isnumeric() function. Remember to remove any '.' characters in the string to identify numeric strings with decimal values.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.