Question
How can I check the type of a variable in Python? For example, how can I inspect whether a value is an integer, string, list, or another Python type? I am also wondering how this relates to specific numeric types such as an unsigned 32-bit value.
Short Answer
By the end of this page, you will understand how Python represents types, how to inspect a variable’s type with type(), when to use isinstance() instead, and why Python’s numeric system is different from fixed-size types like unsigned 32-bit integers in some other languages.
Concept
Python is a dynamically typed language. That means values have types, and variables simply refer to those values.
For example:
x = 10
name = "Ada"
items = [1, 2, 3]
Here:
10is anint"Ada"is astr[1, 2, 3]is alist
To inspect a value’s type, Python provides the built-in type() function:
x = 10
print(type(x))
This prints:
<class 'int'>
That tells you the object referenced by x is an instance of the int class.
Why this matters
Type inspection is useful when:
- debugging unexpected values
- validating function inputs
- working with data from APIs, files, or user input
- writing code that behaves differently for different kinds of values
Python and fixed-size numeric types
In some languages, you may see types like:
uint32int16unsigned long
Python’s built-in int is different. It does not normally store integers as fixed-size 32-bit or 64-bit values. Python integers can grow as large as needed, limited mainly by available memory.
So if you write:
x = 42
print(type(x))
You get:
<class 'int'>
not uint32 or int32.
If you specifically need fixed-width numeric types, those usually come from external libraries such as NumPy, not from standard Python integers.
Mental Model
Think of a variable as a label stuck onto a box.
- The label is the variable name, like
x - The box contents are the actual value, like
42 - The kind of box is the type, like
int
When you ask Python for type(x), you are really asking:
“What kind of value is currently inside the box that
xpoints to?”
And because Python is dynamic, the same label can later point to a different kind of box:
x = 42
print(type(x))
x = "hello"
print(type(x))
Now x first points to an int, then to a str. The variable name did not have a fixed type; the value did.
Syntax and Examples
The two most useful tools are type() and isinstance().
Using type()
value = 3.14
print(type(value))
Output:
<class 'float'>
You can also compare directly:
value = "hello"
print(type(value) == str)
Output:
True
Using isinstance()
value = "hello"
print(isinstance(value, str))
Output:
True
Step by Step Execution
Consider this example:
value = 42
print(type(value))
print(isinstance(value, int))
Step by step:
-
value = 42- Python creates an integer object with the value
42. - The name
valuenow refers to that object.
- Python creates an integer object with the value
-
print(type(value))- Python looks up the object referenced by
value. - It asks for that object’s type.
- The result is
int. - Printed output looks like this:
<class 'int'> - Python looks up the object referenced by
-
print(isinstance(value, int))- Python checks whether the object referenced by
valueis an instance ofint. - This returns .
- Python checks whether the object referenced by
Real World Use Cases
Here are common situations where checking types is useful.
Debugging data problems
If a value is not behaving as expected, type inspection helps you see what you actually received.
data = "123"
print(type(data))
This reveals that the value is a string, not an integer.
Validating function inputs
def double_number(value):
if not isinstance(value, (int, float)):
raise TypeError("value must be numeric")
return value * 2
This prevents invalid input from causing confusing bugs later.
Processing API or JSON data
Data from APIs often contains mixed types.
response = {"count": "5"}
print(type(response["count"]))
The value may look numeric but actually be a string.
Working with collections
Sometimes code needs different behavior depending on the input.
Real Codebase Usage
In real projects, developers usually do not check types everywhere. Instead, they use type checks in a few practical patterns.
Guard clauses
A guard clause rejects invalid input early.
def save_score(score):
if not isinstance(score, int):
raise TypeError("score must be an integer")
print("Saving score:", score)
This keeps the rest of the function simple.
Validation at boundaries
Type checks are common where data enters your program:
- API request handlers
- command-line arguments
- file readers
- form input processors
- configuration loaders
def parse_port(value):
if isinstance(value, str):
value = int(value)
if not isinstance(value, int):
raise TypeError("port must be an integer")
return value
Error handling
Common Mistakes
Mistake 1: Expecting C-style numeric types in standard Python
Broken expectation:
x = 5
print(type(x)) # expecting uint32
Reality:
<class 'int'>
Python’s built-in integers are not usually fixed-width unsigned types.
Mistake 2: Using type(x) == ... when isinstance() is better
Less flexible:
if type(value) == int:
print("integer")
Usually better:
if isinstance(value, int):
print("integer")
isinstance() handles inheritance correctly and is more idiomatic.
Mistake 3: Confusing the printed value with its type
Comparisons
| Approach | What it does | Best use | Example |
|---|---|---|---|
type(x) | Returns the exact type of a value | Inspecting or debugging | type(5) |
type(x) == int | Checks whether the exact type is int | Rarely needed for strict matching | type(x) == int |
isinstance(x, int) | Checks whether a value is an instance of int or a subclass | Most runtime type checks | isinstance(x, int) |
type() vs isinstance()
Cheat Sheet
Quick reference
Inspect a type
type(value)
Example:
type(10) # <class 'int'>
type("hello") # <class 'str'>
type([1, 2, 3]) # <class 'list'>
Check whether a value matches a type
isinstance(value, int)
isinstance(value, str)
isinstance(value, (int, float))
Common built-in Python types
intfloatstrboollisttuple
FAQ
How do I check a variable’s type in Python?
Use type(variable) to see the exact type, or isinstance(variable, SomeType) to check whether it matches a type.
Should I use type() or isinstance() in Python?
Use type() when you want to inspect or print the exact type. Use isinstance() for most type checks in real code.
Why does Python show int instead of uint32?
Python’s built-in integers are not usually fixed-width types. A normal integer is just int, even if its value would fit into 32 bits.
Can a Python variable change type?
Yes. A variable name can refer to an int at one moment and a str later.
How do I check if a value is an integer in Python?
Use:
isinstance(value, int)
How do I inspect a NumPy type like uint32?
Mini Project
Description
Build a small Python script that inspects a list of mixed values and reports the type of each one. This mirrors real debugging work, where data may come from users, files, or APIs and you need to understand what kinds of values you received.
Goal
Create a program that loops through mixed data, prints each value’s type, and labels common Python types clearly.
Requirements
- Create a list containing at least one integer, string, float, list, and boolean.
- Loop through the list and print each value.
- Show the exact type of each value using
type(). - Use
isinstance()to print a friendly label such as "integer" or "string". - Handle unknown types with a default label.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.
Catch Multiple Exceptions in One except Block in Python
Learn how to catch multiple exceptions in one Python except block using tuples, with examples, mistakes, and real-world usage.