The exception or error message TypeError: unsupported operand type(s) for +: 'int' and 'str' appears because an integer and a string operands are operated by the + or addition operator. The operands are the objects of type int and str. This error is usually encountered by people new to Python. This post will discuss this mistake and how to avoid it.
1. What is the problem? #
Let us dissect the exception message TypeError: unsupported operand type(s) for +: 'int' and 'str'.
- TypeError - It is an exception class that tells us there is an error in the code related to the type of the object and the operator used on the object.
- unsupported operand - The operand or items being operated. For example in 2 + 5, the 2 and 5 are called operands and the + is called the operator.
- type(s) - The type() is a built-in function used to get the type or class of an object.
- + - The operator that performs addition if the operands are numeric like int 50 or float 25.5, or concatenation if operands are str.
- 'int' and 'str' - The type of operands, one is of type int and the other is of type str.
Let us look at some examples of how to get and check the type of an object.
How to get the type of an object?
Use the type() built-in function.
var_1 = 100
var_1_type = type(var_1)
print(var_1_type)
output
<class 'int'>
How to check if an object is an int or integer?
Use the insinstance() built-in function.
var_2 = 50
if isinstance(var_2, int):
print(f'var_2 is of type int')
output
var_2 is of type int
How to check if an object is an str or string?
Use the isinstance() built-in function.
var_3 = 'east'
if isinstance(var_3, str):
print(f'var_3 is of type str')
output
var_3 is of type str
Let us try to duplicate the exception message.
# Define our variables.
num_fruits_oranges = 7
num_fruits_apples = '3'
# Get the sum from our variables.
total_fruits = num_fruits_oranges + num_fruits_apples
print(f'total fruits: {total_fruits}')
output
Traceback (most recent call last):
File "f:\Project\kodlogs\art4.py", line 7, in <module>
total_fruits = num_fruits_oranges + num_fruits_apples
TypeError: unsupported operand type(s) for +: 'int' and 'str'
TypeError: 'TypeError: unsupported operand type(s) for +: 'int' and 'str'
We have two operands, num_fruits_oranges of type int and num_fruits_apples of type str. The + operator attempts to add the operands. The TypeError occurred because Python does not support the addition between int and str objects.
2. How to avoid this error? #
To avoid this type of exception, we need to know the types of objects to add. If one object is a string and the other is an integer, we should not add them. Integers are whole numbers and can have a value of negative, positive and 0 values. Strings are characters enclosed in single and double quotes. A string can also be a single character, such as the letter 'a.'
If for some reason we don't know the type of a variable, we can use the isinstance() built-in function to check if it is an int.
code
def weird_function(isnumber=True):
return 10 if isnumber else 'numeric'
# Get the value of the weird function.
value = weird_function()
# Check if value is an int or not.
if isinstance(value, int):
print('value is of type int')
else:
print('value is not of type int')
output
value is of type int
Once we know that the object is an int, we can then proceed with our addition operation.
There is a possibility that the operand is a numeric string, such as "10". In this scenario, we must first convert the str to an int object before doing any integer-specific operations.
code
# Ask the age, we expect a number.
num_years = input('How old are you? ')
print(f'num_years value is {num_years}')
# Assuming we dont' know the return type of the input function,
# we will use the isinstance() function to know if it is an int.
if isinstance(num_years, int):
print('num_years is an int')
else:
print('num_years is not an int')
# Convert the numeric string to int.
print('Convert the num_years str to int using int().')
num_years_int = int(num_years)
# Check the type of num_years_int.
print(f'num_years_int is of type {type(num_years_int)}')
After the str is successfully converted to int using the built-in function int(), we can then continue operating on it.
sample output
How old are you? 54
num_years value is 54
num_years is not an int
Convert the num_years str to int using int().
num_years_int is of type <class 'int'>
How to check if a variable or object type is int?
Answer: Use the built-in function isinstance(variable, int). If it returns True, it is int.
How to know the type or class of a variable or object?
Answer: Use the built-in function type(variable).
What is the return type of an
input() built-in function?
Answer: The return type is
str.
We will encounter the exception message TypeError: unsupported operand type(s) for +: 'int' and 'str' if we try to add an integer object with the string object. We can only add two or more operands if they are numeric such as int or float. We can avoid this errror by knowing the object type. We can use the isinstance() built-in function to check if the object is of type int or str. We can also determine the type or class of the object by using another built-in function type().
- Python-Dev. (n.d.-a). built-in-functions. Retrieved November 10, 2022, from https://docs.python.org/3/library/functions.html#built-in-functions
- Python-Dev. (n.d.-b). input. Retrieved November 22, 2022, from https://docs.python.org/3/library/functions.html#input
- Python-Dev. (n.d.-c). int. Retrieved November 22, 2022, from https://docs.python.org/3/library/functions.html#int
- Python-Dev. (n.d.-d). isinstance. Retrieved November 22, 2022, from https://docs.python.org/3/library/functions.html#isinstance
- Python-Dev. (n.d.-e). Text Sequence Type - str. Retrieved November 22, 2022, from https://docs.python.org/3/library/stdtypes.html#textseq
- Python-Dev. (n.d.-f). type. Retrieved November 16, 2022, from https://docs.python.org/3/library/functions.html#type
- Python-Dev. (n.d.-g). TypeError. Retrieved November 15, 2022, from https://docs.python.org/3/library/exceptions.html#TypeError