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: FalseI executed the Python code, and you can see the output in the screenshot below:

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: FalseHere is the output you can see in the screenshot below:

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(): ReturnsTrueif all characters in the string are digits (0-9).isdecimal(): Similar toisdigit()but also includes decimal characters.isnumeric(): ReturnsTrueif 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: FalseCheck 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:
- Python check if a variable is an integer
- Sum of Digits of a Number in Python
- Check if a Float Variable is Empty in Python

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.