How to check the data type of a variable or object in Python

Published on Aug. 22, 2023, 12:16 p.m.

In Python, you can use the type() function to check the data type of a variable or object. Here are some examples:

x = 42
print(type(x))  # <class 'int'>

y = 3.14
print(type(y))  # <class 'float'>

z = "Hello, world!"
print(type(z))  # <class 'str'>

The type() function returns a type object that represents the data type of the variable or object. You can use this function to check the data type of any variable in your Python code.

I hope this helps! Let me know if you have any further questions.

Tags:

related content