# How To Check If The Variable is An Integer in Python

Do you want to check if the variable is an Integer in Python? This article will show you how you can check a variable for Integer using various methods available.

Suppose you have a variable and you want to do some math on that variable if the input or that particular variable is set to an integer only otherwise you want to throw an error message or skip the further operation related to that variable.

You can easily do that using the is_digit(), isinstance(), and is_integer() method of Python. Let me show you below example code.

## Using isdigit Method to Check if Variable is Integer

If your variable is getting stored in a default mode that is the string in Python then you can check that variable is an integer or not using the isdigit method. This method will return True if the variable is integer else it will return False.

Let us see that in the below example code usage of isdigit Method.

```#Taking a input from the User as Number
getInputString = input("Enter the Number.\n")

#If the Input String is Number Print the Number
if getInputString.isdigit():
print("The variable is an Integer\n")
else:
print("Error: The given Variable is not Integer")```

Output:

``````Example 1:
Enter the Number.
10
The variable is an Integer

Example 2:
Enter the Number.
coduber
Error: The given Variable is not Integer``````

As you can see the above code throws an error when the variable is not an Integer. And it properly detects if the variable is Integer in the first example input.

## Using isinstance Method to Verify Variable for Being an Integer

If you are already taking the input from the user and casting it in an Integer then you can use the isinstance method from the Math Class that checks if the Variable is an Integer or not. It works similarly to the above method.

Let us see the example code for usage of the is_integer method.

```#Taking a input from the User as Number
listToCheckNumber = [1, "A", 2, "B"]

for isItemNumber in listToCheckNumber:
#If the Input String is Number Print the Number
if isinstance(isItemNumber, int):
print("The variable {} is an Integer.".format(isItemNumber))
else:
print("Error: The given Variable {} is not Integer.".format(isItemNumber))```

Output:

``````The variable 1 is an Integer.
Error: The given Variable A is not Integer.
The variable 2 is an Integer.
Error: The given Variable B is not Integer.``````

As you can see in the above code example, we have a list of items mixed with numbers and alphabets, and using a loop we are checking each item in the list if it is an Integer or not and printing the output based on it.

## Using is_integer Method When The Given Variable is already a Float

If you have a Float Variable and you want to check if it is not having any decimal values and it is only an integer then you can use the method is_integer() to check that. This method will return True if the float number is only integer and False if it has decimal values.

Let us see in the below example code the usage of the is_integer method.

```#Taking a input from the User as Number
floatNumberToCheck = float(input("Enter the Float Value.\n"))

#If the Input String is Number Print the Number
if floatNumberToCheck.is_integer():
print("The variable {} is an Integer.".format(int(floatNumberToCheck)))
else:
print("Error: The given Variable {} is not Integer.".format(floatNumberToCheck))```

Output:

``````#Example 1:
Enter the Float Value.
10
The variable 10 is an Integer.

#Example 2:
Enter the Float Value.
10.1
Error: The given Variable 10.1 is not Integer.``````

As you can see in the above code when we are entering 10 as the value of float variable the output for is_integer is True whereas when we are entering the 10.1 value of float variable then the output for is_integer is False.

## Wrap Up

I hope you got your answer on how to check if the variable is an Integer or not. I have shown you three different ways by which you can achieve the solution to this problem.

Let me know if you have any better method for the above problem I will be happy to add it here on my Website.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.