Last updated: Apr 8, 2024
Reading timeยท13 min
Make sure to click on the correct subheading depending on your error message
The Python "TypeError: unsupported operand type(s) for +: 'int' and 'str'" occurs when we try to use the addition (+) operator with an integer and a string.
To solve the error, convert the string to an integer when using the addition (+) operator.
Here is an example of how the error occurs.
my_int = 10 my_str = '5' # โ๏ธ TypeError: unsupported operand type(s) for +: 'int' and 'str' result = my_int + my_str
We are trying to use the addition (+) operator with an integer and a string.
To solve the error, we have to convert the string to a number (an
int or a float
).
my_int = 10 my_str = '5' result = my_int + int(my_str) print(result) # ๐๏ธ 15
We used the int()
class to convert the string to an integer before using the
addition operator.
If you have a float wrapped in a string, use the float() class instead.
my_int = 10 my_str = '5' result = my_int + float(my_str) print(result) # ๐๏ธ 15.0
The float()
class converts the string to a floating-point number.
If you meant to concatenate two strings, convert the integer to a string instead.
my_str = 'Number: ' my_int = 10 result = my_str + str(my_int) print(result) # ๐๏ธ 'Number: 10'
The values on the left and right-hand sides of the addition (+) operator have to be of compatible types.
In other words, both values have to be numbers or both values have to be strings.
Alternatively, you can use a formatted string literal.
my_str = 'number is' my_int = 10 result = f'The {my_str} {my_int}' print(result) # ๐๏ธ 'The number is 10'
Formatted string literals (f-strings) let us include expressions inside of a
string by prefixing the string with f
.
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # ๐๏ธ bobbyhadz
Make sure to wrap expressions in curly braces - {expression}
.
Note that the input()
built-in function always returns a string, even if the
user enters an integer.
my_int = 50 num = input('Enter a number: ') print(num) # ๐๏ธ 10 print(type(num)) # ๐๏ธ <class 'str'> # โ๏ธ TypeError: unsupported operand type(s) for +: 'int' and 'str' result = my_int + num
The error is caused because the input
function always returns a string even if
the user enters an integer.
You can use the int()
constructor to convert the string to an integer before
adding the values.
my_int = 50 # โ Convert to an integer num = int(input('Enter a number: ')) print(num) # ๐๏ธ 10 print(type(num)) # ๐๏ธ <class 'int'> result = my_int + num print(result) # ๐๏ธ 60
We used the int()
class to convert the user input value to an integer to be
able to add the two numbers.
If you need to extract an integer from a string, use the filter()
function.
int_1 = 100 my_str = 'a5b0' int_2 = int(''.join(filter(str.isdigit, my_str))) print(int_2) # ๐๏ธ 50 result = int_2 + int_1 print(result) # ๐๏ธ 150
The filter() function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.
The str.isdigit() method returns
True
if all characters in the string are digits and there is at least 1
character, otherwise False
is returned.
If you aren't sure what type a variable stores, use the built-in type()
class.
my_int = 100 print(type(my_int)) # ๐๏ธ <class 'int'> print(isinstance(my_int, int)) # ๐๏ธ True my_str = '50' print(type(my_str)) # ๐๏ธ <class 'str'> print(isinstance(my_str, str)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
The Python "TypeError: unsupported operand type(s) for -: 'str' and 'str'"
occurs when we try to use the subtraction -
operator with two strings.
To solve the error, convert the strings to integers before subtracting the two numbers.
Here is an example of how the error occurs.
str_1 = '50' str_2 = '20' # โ๏ธ TypeError: unsupported operand type(s) for -: 'str' and 'str' result = str_1 - str_2
We caused the error by using the subtraction (-) operator with two values of type string.
To solve the error, we have to convert the strings to numbers (e.g. integers or floats).
str_1 = '50' str_2 = '20' result = int(str_1) - int(str_2) print(result) # ๐๏ธ 30
We used the int()
class to convert the strings to integers before using the
subtraction operator.
If you have floating-point numbers wrapped in strings, use the float()
class
instead.
str_1 = '50' str_2 = '20' result = float(str_1) - float(str_2) print(result) # ๐๏ธ 30.0
The float()
constructor takes a value and converts it to a floating-point
number.
Note that the input()
function wraps the user-provided value into a string and
returns the result.
num1 = input('Enter a number: ') print(num1) # ๐๏ธ 10 print(type(num1)) # ๐๏ธ <class 'str'> num2 = input('Enter another number: ') print(num2) # ๐๏ธ 5 print(type(num2)) # ๐๏ธ <class 'str'> # โ๏ธ TypeError: unsupported operand type(s) for -: 'str' and 'str' result = num1 - num2
The input()
function is always guaranteed to return a string even if the user
enters a number.
To solve the error, use the int()
class to convert the strings to integers
before subtracting.
# โ Convert to an integer num1 = int(input('Enter a number: ')) print(num1) # ๐๏ธ 10 print(type(num1)) # ๐๏ธ <class 'int'> # โ Convert to an integer num2 = int(input('Enter another number: ')) print(num2) # ๐๏ธ 5 print(type(num2)) # ๐๏ธ <class 'int'> result = num1 - num2 print(result) # ๐๏ธ 5
We used the int()
class to convert the two input values to integers before
using the subtraction (-) operator.
If you need to take floating-point numbers, use the float()
class instead.
If you have a string that may also contain characters but you only need to
extract an integer, use the filter()
function to filter out all non-digits.
str_1 = 'a 5 b 0' num_1 = int(''.join(filter(str.isdigit, str_1))) print(num_1) # ๐๏ธ 50 str_2 = '20' result = num_1 - int(str_2) print(result) # ๐๏ธ 30
The filter() function takes a function and an iterable as arguments and constructs an iterator from the elements of the iterable for which the function returns a truthy value.
The str.isdigit() method returns
True
if all characters in the string are digits and there is at least 1
character, otherwise, False
is returned.
If you aren't sure what type a variable stores, use the built-in type()
class.
str_1 = '50' print(type(str_1)) # ๐๏ธ <class 'str'> print(isinstance(str_1, str)) # ๐๏ธ True int_1 = 20 print(type(int_1)) # ๐๏ธ <class 'int'> print(isinstance(int_1, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed in class.
The Python "TypeError: unsupported operand type(s) for /: 'list' and 'int'"
occurs when we try to use the division /
operator with a list and a number.
To solve the error, access a specific value in the list or use a for
loop to
divide each item.
Here is an example of how the error occurs.
my_list = [10, 20, 30] my_int = 5 # โ๏ธ TypeError: unsupported operand type(s) for /: 'list' and 'int' result = my_list / my_int
Using the division /
operator with a list and an integer causes the error.
list
and correct the assignment.If you need to iterate over the list
and divide each number, use a
for loop.
my_list = [10, 20, 30] my_int = 5 new_list = [] for num in my_list: result = num / my_int print(result) # ๐๏ธ 2.0, 4.0, 6.0 new_list.append(result) print(new_list) # ๐๏ธ [2.0, 4.0, 6.0]
We used a for
loop to iterate over the list and used the division /
operator
to divide each number by a constant.
You can optionally store the results in a new list as shown in the code sample.
Alternatively, you can use a list comprehension.
my_list = [10, 20, 30] my_int = 5 new_list = [num / my_int for num in my_list] print(new_list) # ๐๏ธ [2.0, 4.0, 6.0]
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
On each iteration, we divide the current number by a constant and return the result.
If you need to divide a specific list item by a value, access the list at an index.
my_list = [10, 20, 30] my_int = 5 result = my_list[0] / my_int print(result) # ๐๏ธ 2.0
We accessed the list item at index 0
and divided it by 5
.
Python indexes are zero-based, so the first item in a list has an index of 0
,
and the last item has an index of -1
or len(a_list) - 1
.
If you need to get the sum of the list, use the sum()
function.
my_list = [10, 20, 30] my_int = 5 result = sum(my_list) / my_int print(result) # ๐๏ธ 2.0 print(sum(my_list)) # ๐๏ธ 60
The sum function takes an iterable, sums its items from left to right and returns the total.
If you aren't sure what type a variable stores, use the built-in type()
class.
my_list = [10, 20, 30] print(type(my_list)) # ๐๏ธ <class 'list'> print(isinstance(my_list, list)) # ๐๏ธ True my_int = 5 print(type(my_int)) # ๐๏ธ <class 'int'> print(isinstance(my_int, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
The Python "TypeError: unsupported operand type(s) for -: 'int' and
'function'" occurs when we try to use the subtraction -
operator with an
integer and a function.
To solve the error, make sure to call the function, e.g. my_func()
.
Here is an example of how the error occurs.
def get_num(): return 25 # โ๏ธ TypeError: unsupported operand type(s) for -: 'int' and 'function' result = 50 - get_num
We forgot to invoke the get_num
function so we ended up trying to subtract a
function from an integer.
To solve the error, make sure to call the function using parentheses.
def get_num(): return 25 # ๐๏ธ use parentheses to call the function result = 50 - get_num() print(result) # ๐๏ธ 25
We used parentheses to call the function, so we could get its return value.
my_func(10, 20)
.def get_num(a, b): return a + b # ๐๏ธ use parentheses to call the function result = 50 - get_num(10, 15) print(result) # ๐๏ธ 25
You should be returning a number from the function as trying to subtract different types (e.g. a string and a number) would cause an error.
If you aren't sure what type a variable stores, use the built-in type()
class.
def get_num(): return 25 print(type(get_num)) # ๐๏ธ <class 'function'> print(callable(get_num)) # ๐๏ธ True print(type(get_num())) # ๐ <class 'int'> print(isinstance(get_num(), int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
The callable function takes an
object as an argument and returns True
if the object appears callable,
otherwise False
is returned.
If the callable()
function returns True
, it is still possible that calling
the object fails, however, if it returns False
, calling the object will never
succeed.
The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'"
occurs when we try to use the division /
operator with a string and a
number.
To solve the error, convert the string to a number before dividing.
Here is an example of how the error occurs.
my_str = '10' my_num = 5 # โ๏ธ TypeError: unsupported operand type(s) for /: 'str' and 'int' result = my_str / my_num
We are trying to use the division /
operator with a string and a number.
To solve the error, convert the string to a number before using the division operator.
my_str = '10' my_num = 5 result = int(my_str) / my_num print(result) # ๐๏ธ 2.0
We used the int()
class to convert the string to an integer before using the
division operator.
If you have a float wrapped in a string, use the float()
class instead.
my_str = '10' my_num = 5 result = float(my_str) / my_num print(result) # ๐๏ธ 2.0
The division operator /
always produces a float, if you need an integer, you
can use the floor division operator
//
instead.
my_str = '10' my_num = 5 result = int(my_str) // my_num print(result) # ๐๏ธ 2
The result of using the floor division operator is that of a mathematical
division with the floor()
function applied to the result.
The input()
function wraps the supplied value into a string and returns the
result.
Even if the user enters a number, the value is returned as a string.
num_1 = input('Enter a number: ') print(num_1) # ๐๏ธ 9 print(type(num_1)) # ๐๏ธ <class 'str'> num_2 = 3 # โ๏ธ TypeError: unsupported operand type(s) for /: 'str' and 'int' result = num_1 / num_2
Use the int()
class to convert the user input value to an integer before
dividing.
# โ Convert the input to an integer num_1 = int(input('Enter a number: ')) print(num_1) # ๐๏ธ 9 print(type(num_1)) # ๐๏ธ <class 'int'> num_2 = 3 result = num_1 / num_2 print(result) # ๐๏ธ 3.0
We used the int()
class to convert the input value to an integer before
dividing.
Note that dividing integers returns a floating-point number.
If you need to get an integer value, use the floor //
division operator.
result = num_1 // num_2
If you aren't sure what type a variable stores, use the built-in type()
class.
my_str_1 = '10' print(type(my_str_1)) # ๐๏ธ <class 'str'> print(isinstance(my_str_1, str)) # ๐๏ธ True my_int_1 = 2 print(type(my_int_1)) # ๐๏ธ <class 'int'> print(isinstance(my_int_1, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
The Python "TypeError: unsupported operand type(s) for *: 'float' and
'decimal.Decimal'" occurs when we try to use the multiplication operator with a
float and a Decimal
object.
To solve the error, convert the float to a decimal, e.g.
Decimal(my_float) * my_decimal
.
Here is an example of how the error occurs.
from decimal import Decimal my_float = 3.2 my_decimal = Decimal('3.14') # โ๏ธ TypeError: unsupported operand type(s) for *: 'float' and 'decimal.Decimal' result = my_float * my_decimal
We are trying to use the division operator with a Decimal
object and a float.
Since Decimal
objects and floats are handled differently, we have to convert
the floating-point number to a Decimal
object.
from decimal import Decimal my_float = 3.2 my_decimal = Decimal('3.14') result = Decimal(my_float) * my_decimal print(result) # ๐๏ธ 10.04800000000000055777604757
We used the decimal.Decimal class to construct a new Decimal object from a value.
decimal.Decimal
class can be an integer, string, tuple, float, or another Decimal
object.If you call the Decimal
class without passing a value, it returns 0
.
If you aren't sure what type a variable stores, use the built-in type()
class.
from decimal import Decimal my_float = 3.2 print(type(my_float)) # ๐๏ธ <class 'float'> print(isinstance(my_float, float)) # ๐๏ธ True my_decimal = Decimal('3.14') print(type(my_decimal)) # ๐๏ธ <class 'decimal.Decimal'> print(isinstance(my_decimal, Decimal)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.
The Python "TypeError: unsupported operand type(s) for /: 'tuple' and 'int'"
occurs when we try to use the division /
operator with a tuple and a number.
To solve the error, figure out how the variable got assigned a tuple and correct the assignment, or access a specific value in the tuple.
Here is an example of how the error occurs.
my_tuple = 10, 20, 30 my_int = 2 # โ๏ธ TypeError: unsupported operand type(s) for /: 'tuple' and 'int' result = my_tuple / my_int
Using the division /
operator with a tuple and a number causes the error.
tuple
object and correct the assignment.If you declared a tuple by mistake, tuples are constructed in the following ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorIf you need to iterate over the tuple
and divide each number, use a for
loop.
my_tuple = 10, 20, 30 my_int = 2 my_list = [] for num in my_tuple: result = num / my_int print(result) # ๐๏ธ 5.0, 10.0, 15.0 my_list.append(result) print(my_list) # ๐๏ธ [5.0, 10.0, 15.0]
On each iteration, we divide the current tuple element by a number and print the result.
Alternatively, you can use a list comprehension.
my_tuple = 10, 20, 30 my_int = 2 new_tuple = tuple([x / my_int for x in my_tuple]) print(new_tuple) # ๐๏ธ (5.0, 10.0, 15.0)
We used a list comprehension to divide each element of the tuple to a number and converted the result to a tuple.
If you need to use the division operator on an item in the tuple, access the item at its specific index.
my_tuple = 10, 20, 30 my_int = 2 result = my_tuple[0] / my_int print(result) # ๐๏ธ 5.0
We accessed the tuple element at index 0
and divided it by 2
.
Python indexes are zero-based, so the first item in a tuple has an index of 0
,
and the last item has an index of -1
or len(my_tuple) - 1
.
If you need to get the sum of the tuple, use the sum()
function.
my_tuple = 10, 20, 30 my_int = 2 result = sum(my_tuple) / my_int print(result) # ๐๏ธ 30.0 print(sum(my_tuple)) # ๐๏ธ 60.0
The sum function takes an iterable, sums its items from left to right and returns the total.
If you aren't sure what type a variable stores, use the built-in type()
class.
my_tuple = (10, 20, 30) print(type(my_tuple)) # ๐๏ธ <class 'tuple'> print(isinstance(my_tuple, tuple)) # ๐๏ธ True my_int = 2 print(type(my_int)) # ๐๏ธ <class 'int'> print(isinstance(my_int, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance function returns
True
if the passed-in object is an instance or a subclass of the passed-in
class.