How to Check if Input is a Number in Python?

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.

Check if Input is a Number in Python

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.

How to Check if Input is a Number in Python

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 if Input is a Number in Python isdecimal() Method

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.