Python type() Function
type(obj) returns the class of an object, such as <class 'int'> or <class 'str'>, so you can see what kind of value a variable holds.Every value in Python has a type, and type() tells you which one. It is most useful while learning and debugging, for example to find out why \"5\" + 3 fails or what kind of object a function really returned. See the data types lesson for the full tour of built-in types.
For deciding what code to run based on a type, the usual tool is isinstance(), explained below.
| Syntax | type(obj) |
|---|---|
| Parameters | obj: any value |
| Returns | The class (type object) of obj |
| Other form | type(name, bases, namespace) creates a new class; this is advanced and rarely needed |
What type() returns for common values
| Value | type() result | Name only |
|---|---|---|
42 | <class 'int'> | type(42).__name__ gives int |
3.14 | <class 'float'> | float |
'hi' | <class 'str'> | str |
[1, 2] | <class 'list'> | list |
(1, 2) | <class 'tuple'> | tuple |
{'a': 1} | <class 'dict'> | dict |
True | <class 'bool'> | bool |
None | <class 'NoneType'> | NoneType |
type() vs isinstance(): which one to use for checking types
Comparing with type(x) is int accepts only the exact class. That sounds strict, but it surprises you with subclasses: bool is a subclass of int, so True passes isinstance(True, int) yet fails type(True) is int. In most programs you want isinstance(), which respects inheritance and also accepts a tuple of types such as isinstance(x, (int, float)).
flag = True
print(type(flag) is int)
print(isinstance(flag, int))
print(isinstance(3.5, (int, float)))Use type() to display or log a type, and isinstance() to make decisions.
Using type() to debug conversion problems
Many beginner errors come from a value having a different type than you assumed. Text read with input() is always a str, and numbers read from files or CSVs arrive as text too. Printing type(value) shows the cause immediately, and the fix is usually a conversion with int() or float(). The casting lesson covers these conversions.
Examples you can run
Press Run, then edit the code and run it again.
Check the type of different values
Print type() for each kind of value.
print(type(42))
print(type(3.14))
print(type("hello"))
print(type([1, 2, 3]))
print(type(None))Expected output<class 'int'>
<class 'float'>
<class 'str'>
<class 'list'>
<class 'NoneType'>
Find out why arithmetic gives a surprise
Both values print as 10, but one is text. The types reveal it.
quantity = "10"
stock = 10
print(quantity, stock)
print(type(quantity).__name__, type(stock).__name__)
print(quantity == stock)Expected output10 10
str int
False
Handle several types with isinstance()
A small helper that treats ints and floats the same way.
def describe(value):
if isinstance(value, (int, float)):
return "number"
if isinstance(value, str):
return "text"
return "other"
print(describe(7), describe(2.5), describe("hi"), describe([]))Expected outputnumber number text other
Common mistakes
Comparing type() with a string
✗ Wrong
x = 5
print(type(x) == "int")✓ Right
x = 5
print(type(x) == int)
print(isinstance(x, int))Compare with the class itself, or use isinstance().
Naming a variable type
✗ Wrong
type = "fruit"
print(type(42))✓ Right
kind = "fruit"
print(type(42))Your variable hides the built-in function. Pick another name such as kind or item_type.
Rejecting bool when checking for int
✗ Wrong
flag = True
print(isinstance(flag, int))✓ Right
flag = True
print(isinstance(flag, int) and not isinstance(flag, bool))bool is a subclass of int. If you must exclude it, test isinstance(flag, bool) first.
Python type() Function: questions people ask
How do I check the type of a variable in Python?
Call type(variable), for example type(price) shows <class 'float'>. Add .__name__ to get just float.
What is the difference between type() and isinstance()?
type() returns the exact class, while isinstance() also accepts subclasses and several types at once. Prefer isinstance() for checks.
How do I print only the type name in Python?
Use type(x).__name__, which gives a plain string such as 'list' instead of <class 'list'>.
Keep learning
Step-by-step lessons:
Python Data TypesPython Casting
Related:
Python isinstance() FunctionPython int() FunctionPython str() FunctionPython Mutable vs Immutable Objects Explained
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() FunctionPython sum() Function See all →