W3docs

Determine the type of an object?

To determine the type of an object in Python, you can use the type function.

To determine the type of an object in Python, you can use the type function. For example:

Determine the type of an object in Python using the type function

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

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

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

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

Determine the type of an object in Python using the isinstance function

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

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