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 How to Fix ""zsh: command not found: python" Error on MacOS X? python How to Fix "xlrd.biffh.XLRDError: Excel xlsx file; not supported" Error in Pandas? python How to Remove All Whitespace From a Python String?