How to Check if a Variable is an Integer

5/5 - (4 votes)

Problem Formulation and Solution Overview

In this article, you’ll learn how to check if a variable is an integer data type in Python.

During your career as a Pythonista, you will encounter many times where you need to test if a variable is an integer or not.

πŸ’¬ Question: How would we write Python code to check if a variable is an integer?

We can accomplish this task by one of the following options:


Preparation

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import six

Method 1: Use instance()

This method uses Python’s built-in isinstance() function, which takes two (2) arguments: an Object (variable) and a Class (data type). The variable is checked against the data type and returns a Boolean (True/False) value based on the outcome.

var_a = 11
var_b = ""
print(isinstance(var_a, int))   
print(isinstance(var_b, int))   

πŸ’‘Β Note: You could also pass a Tuple of Classes, such as,
isinstance(object, (class_A, class_B, ...)).

This code declares two (2) variables, var_a, and var_b. Each variable is assigned a value.

Next, the two (2) print statements call isinstance() and pass two (2) arguments to each:

  • The variable declared earlier (var_a or var_b), and
  • The data type (int) to validate against.

Finally, the return value (True/False) are output to the terminal based on the outcomes.

Python isinstance() - A Helpful Guide with Examples

Output

The variable var_a is, in fact, an integer, so this resolves to True.
The variable var_b is an empty string, so this resolves to False.

True
False

Method 2: Use type()

This method uses the type() function to validate the data type and return a Boolean value (True/False) based on the outcome.

var_a = 11
var_b = ""
if type(var_a) == int: print(True)
if type(var_b) != int: print(False)

This code declares two (2) variables, var_a, and var_b. Each variable is assigned a value.

Next, the two (2) print statements call type(), and pass the appropriate variable, var_a, or var_b. Then, the if statement validates the data type (type(var_a), or type(var_b)) against the specified operators and return a Boolean (True/False) value.

Finally, the return values are output to the terminal based on the outcomes.

Python type() Function

Output

True
False

Method 3: Use the moduloΒ (%) operator

This method uses the modulo operator to validate that the variable is an integer. It returns the remainder of dividing the contents of var_a (11) by the value on the right (1). A Boolean value (True/False) returns based on the outcome.

var_a = 11

if var_a % 1 == 0:          
    print(True)
else:
    print(False)

This code declares var_a and is assigned a value.

Next, an if statement is declared and validates to see if applying the modulo operator (if var_a % 1 == 0:) results in zero (0). If so, True is output to the terminal. Otherwise, False is output.

Python Modulo - A Simple Illustrated Guide

Output

True

Method 4: Use try/except

This method uses try/except to test the variable, in this case, var_a to see if it is an integer data type. The outcome depends on the evaluation.

var_a = 11

try:
    int(var_a)
    print(True)
except ValueError: 
    print(False)

This code declares var_a and is assigned a value.

When this code is run, it falls to the try statement where it determines if the variable is an integer. If so, True is output to the terminal. Otherwise, the code falls to except where False is output.

Python Try Except: An Illustrated Guide

Output

True

Method 5: Use six.integer_types

This method calls in six, a Python 2 & 3 compatibility library used to smooth out the differences between the versions.

var_a = 11
if isinstance(var_a, six.integer_types):
    print('var_a is an integer!')

This code declares var_a and is assigned a value.

Then, an if statement uses isinstance() passing two (2) arguments: an object (var_a), and a way to validate the data type (six.integer_types). If this results in True, the print statement is output to the terminal.

Output

var_a is an integer!

Summary

These five (5) methods of checking if a variable is an integer should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!