Python Variables – Check whether a variable is an integer

In this post we will be checking whether a variable in Python is of integer type, to do this we will be using Pythons inbuilt ‘isinstance()’ function.

See the snippet of code below for how this is done. Here our variable is the number ‘9’ and so we would expect this to return as True.

Var=9

Check=isinstance(Var, int)
print(Check)

#Returns True

The snippet of code below is the same although the variable in this case is the text ‘Hello World’ and so we would expect this to return as False.

Var="Hello World"

Check=isinstance(Var, int)
print(Check)

#Returns False 

Leave a Reply