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 = 123def 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()
:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours