How to Determine the Type of a Python Variable?


You can determine the type of a Python variable using the built-in function type(). For example:

x = 5
print(type(x))  # Output: <class 'int'>

y = 2.5
print(type(y))  # Output: <class 'float'>

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

a = [1, 2, 3]
print(type(a))  # Output: <class 'list'>

In each case, type() returns an object that represents the data type of the variable. The object is of type type, so you'll see the output as <class 'type'>.

Latest Questions

python What is the null object in Python? python How to obtain the full path of the current file's directory in Python? python How to print a variable and a string on the same line in Python?