Python has isinstance() is an inbuilt method that is often used to check any type of data (object). Another method can use the type() built-in method.
Python check data type Example
Simple python example code.
type() method
Type() method returns the class type of the data value(object) passed as a parameter.
str1 = "Hello World"
age = 50
pi = 3.14
c_num = 3j + 10
my_list = ["A", "B", "C", "D"]
my_tuple = ("A", "B", "C", "D")
my_dict = {1: "a", 2: "b", 3: "c", 4: "d"}
my_set = {'A', 'B', 'C', 'D'}
print(type(str1))
print(type(age))
print(type(pi))
print(type(c_num))
print(type(my_list))
print(type(my_tuple))
print(type(my_dict))
print(type(my_set))
Output:
isinstance() Method
Use it if you want to check the specific type of data (given variable) is or not. Let’s see data type is a string or not.
message = isinstance("Hello World", str)
print(message)
Output: True
Do comment if you have any doubts and suggestions on this Python data type code.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.