How to check if variable is int (float, str…) in Python

In order to check variable type, just use the following (mind replacing <var> with your variable name):

if isinstance( <var>, int) :
    print("I'm int")
else:
    print("I'm not int")

However, the function is slightely quicker as it isinstance() if just a wrapper around .is_integer() call .

if <var>.is_integer() :
    print("I'm int")
else:
    print("I'm not int")

One can also use the following appoach:

if type(<var>) == int :
    print("I'm int")
else:
    print("I'm not int")

Similary, one can use it same approaches for float or str and other standart types:


isinstance( <var>, float)
isinstance( <var>, str)

So, python offers three choicefor your coding style and convinience!