Python isinstance() function

Last updated Feb 16, 2022

Checking the data type of a certain value is a typical activity in programming. For instance, you might want to confirm if the value being stored in a variable is a text or an integer. This is because every one of these data types operates uniquely in code. That's where the isinstance() method comes in. The isinstance() is a Python built-in method for determining the data type of a given value. For instance, you may use isinstance() to determine whether a value is a list or a string.
The isinstance() method typically provides True or False based upon whether the value under review is stored as the specified data type or types.

Syntax
The syntax of isinstance() is as follows:

isinstance(object, classdescription)

where:
Object: the object whose class is to be checked
Classdescription- the object's description, including class, turple, type, classes, etc.

Example

Consider the following example. We'll see whether the data type of number 3 is correct.
 

a = 3
print("Is 3 an integer? : " + str(isinstance(a, int)))
print("Is 3 a string? : " + str(isinstance(a, str)))


Output
If you run the above code

Is 3 an integer? : True
Is 3 a string? : False

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

255 Views