How to Check Data Type in Python

To check data type in Python, you can use the type() or isinstance() function.

The following examples show how to check data type in Python.

Using type() Function

We can use the type() function to check the data type of a variable.

Suppose we have the following variables:

# Declare variables
a = ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse']
b = 4500
c = True
d = "Laptop"

# Check data type
print(type(a))
print(type(b))
print(type(c))
print(type(d))

Output: 👇️

<class 'list'>
<class 'int'>
<class 'bool'>
<class 'str'>

In this example, we use the type() function to check the data type of variables a, b, c, and d.

Using isinstance() Function

We can use the isinstance() function to check if a variable is of a specific data type.

Suppose we have the following variables:

# Declare variables
a = ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse']
b = 4500
c = True
d = "Laptop"

# Check data type
print(isinstance(a, list))
print(isinstance(b, int))
print(isinstance(c, bool))
print(isinstance(d, str))

Output: 👇️

True
True
True
True

In this example, we use the isinstance() function to check if variables a, b, c, and d are of types list, int, bool, and str respectively.

Conclusion

We can use the type() and isinstance() functions to check data types in Python.