Tells you the type of a value.
Synopsis
type(value)
Description
Use type when a value does not behave the way you expect. It is a debugging tool.
Parameters
| Name | Meaning |
|---|---|
value |
Any value. |
Return value
The type, shown as <class 'int'>, <class 'str'> and so on.
Example
print(type(42))
print(type("42"))
print(type(4.2))
print(type(True))
print(type([1, 2]))
It prints:
<class 'int'>
<class 'str'>
<class 'float'>
<class 'bool'>
<class 'list'>
Common mistake
x = input("Number: ")
print(type(x))
It prints:
Number: 5
<class 'str'>
Not an error, but a surprise: the user typed a number, yet x is a string. input always returns str.
See also
Last updated 2 weeks ago