Python type() Function Explained
The Python type() function tells you what kind of value an object is.
Beginners usually use type() to:
- check whether a value is a string, number, list, or dictionary
- understand what a variable currently contains
- debug confusing code
- inspect values returned by functions such as
input()
In most beginner code, you will use the one-argument form:
type(object)
Python also has a three-argument form of type(), but that is an advanced feature for creating classes dynamically.
Quick answer
name = "Alice"
print(type(name))
number = 10
print(type(number))
Output:
<class 'str'>
<class 'int'>
Use type(value) to see the type of a value or variable. Output will look like <class 'str'> and <class 'int'>.
What the type() function does
type() returns the type of an object.
A type tells you what kind of value something is. For example:
strfor textintfor whole numbersfloatfor decimal numberslistfor listsdictfor dictionariesboolforTrueandFalse
This is especially useful while debugging. If your code is behaving strangely, type() can help you confirm what kind of value you are actually working with.
Basic syntax
The main beginner form is:
type(object)
It returns the class of the object.
Example:
print(type("hello"))
Output:
<class 'str'>
You can pass either:
- a literal value such as
"hello"or42 - a variable such as
nameorage
Simple examples
Here are a few common examples.
print(type("hello"))
print(type(10))
print(type(3.14))
print(type([1, 2, 3]))
print(type({"name": "Alice"}))
print(type(True))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'list'>
<class 'dict'>
<class 'bool'>
These results show the exact type of each value.
If you are still learning Python data types, see Python data types overview.
Using type() with variables
In Python, variables do not have one fixed type forever.
A variable can point to different kinds of values at different times.
value = 10
print(value, type(value))
value = "ten"
print(value, type(value))
value = [10]
print(value, type(value))
Output:
10 <class 'int'>
ten <class 'str'>
[10] <class 'list'>
type() shows the current type of the value stored in the variable.
This is helpful when values come from user input or conversions.
user_input = input("Enter a number: ")
print(type(user_input))
converted = int(user_input)
print(type(converted))
If the user enters 25, the output will be:
<class 'str'>
<class 'int'>
That happens because input() always returns a string. You must convert it with a function like int() or float() if you need a number.
For a step-by-step guide, see how to convert user input to numbers in Python.
type() vs isinstance()
type() and isinstance() are related, but they do different jobs.
type()tells you the exact typeisinstance()checks whether a value matches a type
Example:
value = 5
print(type(value))
print(isinstance(value, int))
Output:
<class 'int'>
True
For many beginner condition checks, isinstance() is more practical.
value = 5
if isinstance(value, int):
print("This is an integer")
Use type() when you want to inspect or print the exact type.
Use isinstance() when you want to check a type inside logic such as if statements.
See the dedicated guide for more detail: Python isinstance() function explained.
The three-argument form of type()
Python also supports this advanced form:
type(name, bases, dict)
This creates a new class dynamically.
Example:
Person = type("Person", (), {"species": "human"})
print(Person)
print(Person.species)
Output:
<class '__main__.Person'>
human
Most beginners do not need this form.
It is mainly used in advanced Python code involving classes and metaprogramming. For everyday learning and debugging, the one-argument form is the important one.
When to use type()
type() is useful when you want to:
- debug unexpected values
- check what a conversion produced
- learn Python data types
- inspect objects while reading examples
For example:
text = "123"
number = int(text)
print(type(text))
print(type(number))
Output:
<class 'str'>
<class 'int'>
This helps you see the difference between text and numbers clearly.
If you need to convert values after checking them, functions like str(), int(), and float() are often the next step.
Common beginner confusion
A few things about type() often confuse beginners.
type(5) does not return plain text
This:
print(type(5))
prints:
<class 'int'>
It does not print just int.
That is because type() returns a class object.
input() returns str
This often surprises beginners:
value = input("Enter something: ")
print(type(value))
No matter what the user types, the result is a string unless you convert it.
If you need a number, use int() or float().
Comparing type names as strings is usually not the best idea
Avoid code like this:
value = 5
if str(type(value)) == "<class 'int'>":
print("It is an int")
This works, but it is not a good approach.
Better options:
value = 5
print(type(value) == int)
print(isinstance(value, int))
Output:
True
True
In most real code, isinstance() is the more useful check.
Common mistakes
Here are some common problems beginners run into with type():
- Using
type()to check user input and forgetting thatinput()always returns a string - Expecting
type()to return plain text likeintinstead of<class 'int'> - Using
type(x) == SomeTypewhenisinstance(x, SomeType)is more appropriate - Confusing the basic one-argument form with the advanced class-creation form
If your problem is really about bad input or failed conversion, you may also want to read TypeError vs ValueError in Python explained.
Useful debugging commands:
print(type(value))
print(value)
print(repr(value))
help(type)
dir(value)
These can help you understand both the value and the object you are working with.
FAQ
What does type() return in Python?
It returns the type of an object, such as str, int, list, or dict, shown as a class object like <class 'str'>.
Is type() the same as isinstance()?
No. type() shows the exact type. isinstance() checks whether a value is an instance of a type.
Why does type(input("Enter: ")) show str?
Because input() always returns text. Convert it with int() or float() if you need a number.
Can type() create classes?
Yes, in its three-argument form, but most beginners only need type(object).