HOWTO · Python

How to Check Variable Type in Python

Learn how to inspect a value's type with Python's type() and isinstance() functions.

On this page

Use type(value) to see the value’s type, or isinstance(value, SomeType) to test whether it belongs to a type (including a subclass). Python variables are names bound to objects, so you inspect the value rather than a declared variable type.

Variables and Their Types in Python

A Python name refers to an object, and the object has a type. Assigning a value binds a name to that object; you do not declare the name’s type in advance. For example, 42, 3.5, "hello", True, and [1, 2] are values of types int, float, str, bool, and list respectively. The same name can later refer to an object of another type.

Check the Type of a Variable in Python

For a quick inspection, pass the value to type(); use isinstance() when your code needs a Boolean type test. The example below shows both approaches with an integer and a string. It is a self-contained example; run it with Python to see the printed results.

count = 50
label = "Ada"

print(type(count))
print(type(label))
print(isinstance(count, int))
print(isinstance(label, int))
<class 'int'>
<class 'str'>
True
False

Use the type() Function to Check Variable Type in Python

The built-in type() function returns the type object for the value you pass. When printed, common built-in types appear in forms such as <class 'int'>, <class 'float'>, and <class 'str'>. This is useful for inspecting a value while debugging or choosing behavior based on its exact type.

For example, type(50) is int, type(10.5) is float, and type("Ada") is str. Calling type() reports what an object is; it does not convert the object to another type.

Use the isinstance() Function to Check Variable Type in Python

Use isinstance(value, classinfo) to get True or False for whether value is an instance of the specified class or classes. Unlike comparing the result of type(value) directly with a class, isinstance() also recognizes subclasses. This makes it a good choice for checks that should accept a class and its descendants.

class Animal:
    pass

class Dog(Animal):
    pass

pet = Dog()
print(isinstance(pet, Dog))
print(isinstance(pet, Animal))
print(type(pet) is Dog)
True
True
True

In this example, both isinstance() checks are true because Dog derives from Animal; the exact-type comparison is true because pet was created as a Dog. Use type(value) is SomeType only when an exact type match is specifically required. Type inspection and instance checking do not change a value’s type.

This distinction matters when a function is designed to work with a base class and any specialized objects built from it. For example, code that accepts an Animal can often handle a Dog through the same interface, so isinstance(pet, Animal) expresses that contract. An exact check such as type(pet) is Animal would reject a Dog, even though it inherits from Animal. Reserve exact-type checks for cases where subclasses must be treated differently or are explicitly unsupported; for ordinary polymorphic code, the subclass-aware check is usually more flexible.