How to Check If Input is a Number in Python

To check if input is a number in Python, you can use the isdecimal() or isnumeric() function.

The following examples show how to check if input is a number in Python.

Using isdecimal() Function

We can use the isdecimal() function to check if input is a number.

Suppose we have the following code:

# Take user input
user_input = input("Enter a number: ")

# Check if input is a number
if user_input.isdecimal():
    print("Input is a number!")
else:
    print("Input is not a number!")

Output: 👇️

Enter a number: 123
Input is a number!

In this example, we use the isdecimal() function to check if the user input is a number.

Using isnumeric() Function

We can use the isnumeric() function to check if input is a number.

Suppose we have the following code:

# Take user input
user_input = input("Enter a number: ")

# Check if input is a number
if user_input.isnumeric():
    print("Input is a number!")
else:
    print("Input is not a number!")

Output: 👇️

Enter a number: 123
Input is a number!

In this example, we use the isnumeric() function to check if the user input is a number.

Conclusion

We can use the isdecimal() and isnumeric() functions to check if input is a number in Python.