isinstance()

Published Apr 2, 2023Updated Aug 8, 2023
Contribute to Docs

The isinstance() function determines whether a given object is of a designated value type. If it is, the function will return True, otherwise the function will return False.

Syntax

isinstance(object, class)
  • object is the object, or a reference to the object, to be tested.
  • class is the type the function will use in this assertion (e.g. list, set, etc.).

Example

var1 = "Hello World!"
var2 = 123
def check_if_string(value):
if isinstance(value, str):
print(f'{value} is a string type.')
else:
print(f'{value} is not a string.')
check_if_string(var1)
check_if_string(var2)

This example results in the following output:

Hello World! is a string type.
123 is not a string.

Codebyte Example

This example is runnable and uses the built-in function isinstance():

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy