Python isinstance() Function Explained
isinstance() checks whether a value matches a type.
It is a built-in Python function that returns either True or False. It is often used before running code that depends on a value being a string, number, list, tuple, or dictionary.
In many cases, isinstance() is a better choice than checking type() directly because it also works with parent and child types.
Quick answer
value = 10
print(isinstance(value, int)) # True
print(isinstance(value, str)) # False
Use isinstance(object, classinfo) to check whether a value matches a type or one of its parent types.
What isinstance() does
isinstance() is used to test a value against a type.
It:
- Checks whether a value is an instance of a type
- Returns
TrueorFalse - Helps you safely run type-specific code
- Is commonly used with
str,int,list,tuple, anddict
This is useful when your code needs to behave differently for different kinds of data.
For example, you might want to:
- loop through a list
- join text only if a value is a string
- do math only if a value is a number
If you need a refresher on Python types, see Python data types overview or what a data type means in Python.
Basic syntax
The syntax is:
isinstance(object, classinfo)
Parts of the syntax
objectis the value you want to testclassinfois the type to check against, such asint,str,list, ordict- The result is always a Boolean value:
TrueorFalse
Example:
name = "Maya"
print(isinstance(name, str))
Output:
True
Simple example
Here is a small example that checks one value at a time.
Check an integer
value = 25
print(isinstance(value, int))
print(isinstance(value, str))
Output:
True
False
Check text
text = "hello"
print(isinstance(text, str))
print(isinstance(text, int))
Output:
True
False
In both examples, isinstance() only checks the type. It does not change the value.
If you want to change a value from one type to another, that is conversion, not type checking. For example, see how to convert a string to an integer in Python.
Checking against multiple types
The second argument can be a tuple of types.
This is helpful when more than one type is acceptable.
value = 3.5
print(isinstance(value, (int, float)))
print(isinstance(value, (str, list)))
Output:
True
False
This is much cleaner than writing long or conditions.
Instead of this:
value = 3.5
print(type(value) == int or type(value) == float)
you can write this:
value = 3.5
print(isinstance(value, (int, float)))
A common use case is accepting both integers and decimal numbers as numeric input.
isinstance() vs type()
Both isinstance() and type() are related to types, but they are not the same.
type(value) == int
This checks for one exact type.
value = 10
print(type(value) == int)
isinstance(value, int)
This checks whether the value matches that type, including inherited types.
value = 10
print(isinstance(value, int))
For beginners, isinstance() is usually the safer choice when you want to know whether a value can be treated like a certain type.
In everyday code:
- use
type()when you need the exact type - use
isinstance()when you want a practical type check
Common real uses
Here are some common beginner-friendly uses for isinstance().
Validate function inputs
def repeat_text(text, count):
if not isinstance(text, str):
return "text must be a string"
if not isinstance(count, int):
return "count must be an integer"
return text * count
print(repeat_text("Hi ", 3))
print(repeat_text("Hi ", "3"))
Output:
Hi Hi Hi
count must be an integer
This can help prevent errors such as unsupported operand type errors.
Handle strings and lists differently
value = ["a", "b", "c"]
if isinstance(value, str):
print("This is text")
elif isinstance(value, list):
print("This is a list")
Output:
This is a list
Check data before converting
value = "42"
if isinstance(value, str):
number = int(value)
print(number)
Output:
42
This is useful when working with user input, since input often starts as text.
Prevent errors before using methods or loops
value = {"name": "Ana"}
if isinstance(value, dict):
print(value["name"])
Output:
Ana
Checking first can help you avoid confusing TypeError messages later.
Common beginner mistakes
Passing "int" instead of int
Wrong:
value = 10
print(isinstance(value, "int"))
"int" is a string, not a type.
Correct:
value = 10
print(isinstance(value, int))
Using square brackets for multiple types
Wrong:
value = 5
print(isinstance(value, [int, float]))
The second argument should be a type or a tuple of types.
Correct:
value = 5
print(isinstance(value, (int, float)))
Thinking isinstance() converts a value
Wrong idea:
value = "123"
result = isinstance(value, int)
print(result)
Output:
False
This does not convert "123" into 123. It only checks the type.
If your goal is conversion, use int(value) instead.
Assuming it changes the value
isinstance() does not modify anything.
value = "100"
print(isinstance(value, str))
print(value)
Output:
True
100
The value stays the same.
Debugging tips
If you are not sure what kind of value you have, these checks are useful:
print(type(value))
print(isinstance(value, int))
print(isinstance(value, (int, float)))
print(repr(value))
These can help when:
- a value looks like a number but is actually a string
- you are getting a
TypeError - your condition is not behaving as expected
- the second argument to
isinstance()is invalid
Common causes of mistakes include:
- using
'int'or'str'as text instead ofintorstr - expecting
isinstance()to convert a value - using
type()when inherited types should also match - passing an invalid second argument
If you get an error because Python expected a number but found text, see TypeError: 'str' object cannot be interpreted as an integer.
FAQ
What does isinstance() return in Python?
It returns True if the value matches the given type, otherwise False.
Can isinstance() check more than one type?
Yes. Pass a tuple of types, such as isinstance(x, (int, float)).
What is the difference between isinstance() and type()?
type() gives the exact type. isinstance() checks whether a value matches a type and also supports inheritance.
Does isinstance() convert a value?
No. It only checks the type. It does not change the value.