How to determine the type of an object in Python?

Better Stack Team
Updated on February 3, 2023

You can use the type() function to determine the type of an object in Python. For example:

 
x = 10
print(type(x))  # Output: <class 'int'>

y = "hello"
print(type(y))  # Output: <class 'str'>

z = [1, 2, 3]
print(type(z))  # Output: <class 'list'>

You can also use the isinstance() function to check if an object is an instance of a particular class or a subclass of it. For example:

 
x = 10
print(isinstance(x, int))  # Output: True
print(isinstance(x, str))  # Output: False

y = "hello"
print(isinstance(y, str))  # Output: True
print(isinstance(y, list))  # Output: False

z = [1, 2, 3]
print(isinstance(z, list))  # Output: True
print(isinstance(z, tuple))  # Output: False

You can also use the issubclass() function to check if a class is a subclass of another class. For example:

 
class A:
    pass

class B(A):
    pass

print(issubclass(B, A))  # Output: True
print(issubclass(A, B))  # Output: False
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack