xxxxxxxxxx
obj = # Replace ... with the object you want to check the type of
obj_type = type(obj)
print(obj_type)
xxxxxxxxxx
>>> type([]) is list
True
>>> type({}) is dict
True
>>> type('') is str
True
>>> type(0) is int
True
xxxxxxxxxx
# To check the type of a variable
print( type(price) )
# To check if a veriable is an instance of something (float in this case)
print( isinstance(price, float) )
xxxxxxxxxx
# To check if the type of o is exactly str, excluding subclasses of str:
if type(o) is str:
# Use isinstance to check if o is an instance of str or any subclass of str:
if isinstance(o, str):
xxxxxxxxxx
# Example variables
var1 = "Hello"
var2 = 123
var3 = True
var4 = 3.14
# Check type of variables
print(type(var1)) # <class 'str'>
print(type(var2)) # <class 'int'>
print(type(var3)) # <class 'bool'>
print(type(var4)) # <class 'float'>