In this tutorial, I will explain how to check if input is a number in Python. As a Python developer working on a project for one of my New York clients, I came across a scenario where I needed to check if the input is a number then I discovered more about this topic. This guide will cover various methods to check if the input is a number with detailed examples and explanations.
Methods to Check if Input is a Number in Python
Python provides several ways to check if a string input is a number. We will explore the most common methods, including using built-in string methods, exception handling, and regular expressions.
Read Interfaces in Python
1. Use the isdigit() Method
The isdigit() method is a built-in string method in Python that checks if all characters in the string are digits. This method is simple but has some limitations, such as not recognizing negative numbers or decimal points.
Example
input_value = input("Enter your zip code: ")
if input_value.isdigit():
print("The input is a valid number.")
else:
print("The input is not a valid number.")Output:
Enter your zip code: I have executed the above example code and added the screenshot below.

In this example, if the user enters “90210”, the program will recognize it as a valid number. However, if the user enters “-90210” or “90210.5”, it will not be recognized as a number.
Check out Access Modifiers in Python
2. Use the isnumeric() Method
The isnumeric() method is similar to isdigit() but also recognizes numeric characters such as fractions and subscript numbers. However, it still does not handle negative numbers or decimal points.
Example
input_value = input("Enter the number of items: ")
if input_value.isnumeric():
print("The input is a valid number.")
else:
print("The input is not a valid number.")Output:
Enter the number of items: I have executed the above example code and added the screenshot below.

This method is useful for more general numeric checks but still falls short for some common use cases.
Check out How to Use Single and Double Quotes in Python?
3. Use the isdecimal() Method
The isdecimal() method checks if all characters in the string are decimal characters. This method is more restrictive than isdigit() and isnumeric(), as it only recognizes decimal numbers.
Example
input_value = input("Enter the price of the item: ")
if input_value.isdecimal():
print("The input is a valid decimal number.")
else:
print("The input is not a valid decimal number.")Output:
Enter the price of the item: I have executed the above example code and added the screenshot below.

Check out Python 3 vs Python 2
4. Use try and except Blocks
One of the most reliable ways to check if a string input is a number is by using a try and except block to attempt to convert the input to an integer or float. This method handles negative numbers and decimal points effectively.
Example for Integer Check
input_value = input("Enter your age: ")
try:
age = int(input_value)
print("The input is a valid integer.")
except ValueError:
print("The input is not a valid integer.")Example for Float Check
input_value = input("Enter the temperature in Fahrenheit: ")
try:
temperature = float(input_value)
print("The input is a valid float.")
except ValueError:
print("The input is not a valid float.")Read Difference Between “is None” and “== None” in Python
5. Use Regular Expressions
Regular expressions provide a powerful way to validate input. You can use the re module in Python to define patterns that match numeric input, including integers, floats, and negative numbers.
Example
import re
input_value = input("Enter your social security number: ")
pattern = r'^-?\d+(\.\d+)?$'
if re.match(pattern, input_value):
print("The input is a valid number.")
else:
print("The input is not a valid number.")In this example, the regular expression pattern r'^-?\d+(\.\d+)?$' matches both integers and floats, including negative numbers.
Read How to Comment Out a Block of Code in Python?
Combine Methods for Robust Validation
Sometimes, you may need to combine multiple methods to achieve robust validation. For example, you might first check if the input is numeric and then convert it to an integer or float within a try and except block.
Example
input_value = input("Enter your house number: ")
if input_value.isnumeric():
try:
house_number = int(input_value)
print("The input is a valid integer.")
except ValueError:
print("The input is not a valid integer.")
else:
print("The input is not a valid number.")Check out Difference Between {} and [] in Python
Conclusion
In this tutorial, I have explained how to check if input is a number in Python. I discussed various methods like using isdigit() method, isnumeric() methods, isdecimal() method, using try and except blocks, regular expression with examples and we also discussed how to combine methods for robust validation.
You may also like to read:
- Compare Lists, Tuples, Sets, and Dictionaries in Python
- Is Python an Object-Oriented Language?
- JavaScript vs Python for Web Development: Choosing the Right for Your Project

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.