isinstance()
mattfwilson1 total contribution
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()
:
All contributors
- mattfwilson1 total contribution
- MMOOAAZZ6 total contributions
Looking to contribute?
- 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.