How to Check if a Variable Contains an Integer in Python?

While working in a Python program, one of my team members needs to validate whether a variable contains an integer. I suggested a few methods, and in this tutorial, I will show you how to check if a variable contains an integer in Python with examples.

To check if a variable contains an integer in Python, you can use the isinstance() function. This function checks if a variable is an instance of a specified data type, returning True if the variable is an integer and False otherwise. For example, isinstance(age, int) will return True if age is an integer.

Check if a Variable Contains an Integer in Python

To check if a variable contains an integer in Python, you can use several methods depending on the variable type. Let me show you a few useful methods:

Using the isinstance() function:

The isinstance() function in Python allows you to check if a variable is an instance of a particular class or data type, including integers. It returns True if the variable is an integer, and False otherwise. This method is useful when you have an unknown type of variable and want to determine if it’s an integer.

Example:

age = 25
price = 19.99
name = "John"

print(isinstance(age, int))    # Output: True
print(isinstance(price, int))  # Output: False
print(isinstance(name, int))   # Output: False

I executed the Python code, and you can see the output in the screenshot below:

check if a variable contains integer in python

Check out Concatenate String and Float in Python

Using the type() function:

The type() function in Python returns the type of an object. You can compare the returned type with the int class to check if a variable is an integer. This method is similar to using isinstance() but provides more specific type information.

Example:

population = 1000000
temperature = 98.6
city = "New York"

print(type(population) == int)    # Output: True
print(type(temperature) == int)  # Output: False
print(type(city) == int)         # Output: False

Here is the output you can see in the screenshot below:

how to check if a variable contains integer in python

Check if a string contains only digits:

When you have a string variable and want to check if it represents an integer, you can use string methods like isdigit()isdecimal(), or isnumeric(). These methods return True if the string contains only digits and can be converted to an integer.

  • isdigit(): Returns True if all characters in the string are digits (0-9).
  • isdecimal(): Similar to isdigit() but also includes decimal characters.
  • isnumeric(): Returns True if the string contains only numeric characters, including digits, subscripts, and superscripts.

Example:

zip_code = "12345"
phone_number = "123-456-7890"
address = "123 Main St"

print(zip_code.isdigit())        # Output: True
print(phone_number.isdigit())    # Output: False
print(address.isdigit())         # Output: False

Check out Convert Int to Bytes in Python

Using exception handling:

In Python, you can also use exception handling with the int() function. If the variable can be converted to an integer, no exception will be raised. Otherwise, a ValueError will occur, indicating that the variable is not an integer. This method is useful when you want to attempt the conversion and handle the case where the variable is not an integer.

Example:

user_input = input("Enter your age: ")

try:
    age = int(user_input)
    print("Your age is:", age)
except ValueError:
    print("Invalid input. Please enter an integer.")

In this tutorial, I explained the most common methods to check if a variable contains an integer in Python. I have added one example for each method, and all the codes are running as expected. I hope this helps.

You may also like:

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.