TypeError: unsupported operand type(s) for +: int and str

avatar
Borislav Hadzhiev

Last updated: Feb 1, 2023
13 min

banner

# Table of Contents

  1. TypeError: unsupported operand type(s) for +: INT and STR
  2. TypeError: unsupported operand type(s) for -: STR and STR
  3. TypeError: unsupported operand type(s) for /: LIST and INT
  4. Unsupported operand type(s) for -: 'INT' and 'FUNCTION'
  5. TypeError: unsupported operand type(s) for /: STR and INT
  6. Unsupported operand type(s) for *: FLOAT and decimal.Decimal
  7. TypeError: unsupported operand type(s) for /: tuple and int

Make sure to click on the correct subheading depending on your error message

# TypeError: unsupported operand type(s) for +: int and str

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.

typeerror unsupported operand type for plus int and str

Here is an example of how the error occurs.

main.py
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.

# Convert the string to an integer to solve the error

To solve the error, we have to convert the string to a number (an int or a float).

main.py
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.

main.py
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.

# Convert the integer to a string to solve the error

If you meant to concatenate two strings, convert the integer to a string instead.

main.py
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.

main.py
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.

main.py
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # ๐Ÿ‘‰๏ธ bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

# The input() function is guaranteed to return a string

Note that the input() built-in function always returns a string, even if the user enters an integer.

main.py
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.

main.py
my_int = 50 # โœ… convert to 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.

# Extracting an integer from a string

If you need to extract an integer from a string, use the filter() function.

main.py
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.

# Checking what type a variable stores

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
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.

# Table of Contents

  1. TypeError: unsupported operand type(s) for -: STR and STR
  2. TypeError: unsupported operand type(s) for /: LIST and INT
  3. Unsupported operand type(s) for -: 'INT' and 'FUNCTION'
  4. TypeError: unsupported operand type(s) for /: STR and INT
  5. Unsupported operand type(s) for *: FLOAT and decimal.Decimal
  6. TypeError: unsupported operand type(s) for /: tuple and int

# TypeError: unsupported operand type(s) for -: str and str

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.

typeerror unsupported operand type for minus str and str

Here is an example of how the error occurs.

main.py
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.

# Convert the strings to numbers before subtracting

To solve the error, we have to convert the strings to numbers (e.g. integers or floats).

main.py
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.

main.py
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.

# The input() function always returns a string

Note that the input() function wraps the user-provided value into a string and returns the result.

main.py
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.

main.py
# โœ… convert to integer num1 = int(input('Enter a number: ')) print(num1) # ๐Ÿ‘‰๏ธ 10 print(type(num1)) # ๐Ÿ‘‰๏ธ <class 'int'> # โœ… convert to 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.

# Extracting an integer from a string

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.

main.py
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.

# Checking the type of the variable

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
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.

# Table of Contents

  1. TypeError: unsupported operand type(s) for /: LIST and INT
  2. Unsupported operand type(s) for -: 'INT' and 'FUNCTION'
  3. TypeError: unsupported operand type(s) for /: STR and INT
  4. Unsupported operand type(s) for *: FLOAT and decimal.Decimal
  5. TypeError: unsupported operand type(s) for /: tuple and int

# TypeError: unsupported operand type(s) for /: list and int

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.

typeerror unsupported operand type for slash list and int

Here is an example of how the error occurs.

main.py
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.

To solve the error, you have to figure out how the variable got assigned a list and correct the assignment.

# Iterating over a list and dividing each number

If you need to iterate over the list and divide each number, use a for loop.

main.py
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.

main.py
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.

# Accessing a specific list element

If you need to divide a specific list item by a value, access the list at an index.

main.py
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.

# Getting the sum of the list

If you need to get the sum of the list, use the sum() function.

main.py
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.

# Checking what type the variable stores

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
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.

# Table of Contents

  1. Unsupported operand type(s) for -: 'INT' and 'FUNCTION'
  2. TypeError: unsupported operand type(s) for /: STR and INT
  3. Unsupported operand type(s) for *: FLOAT and decimal.Decimal
  4. TypeError: unsupported operand type(s) for /: tuple and int

# Unsupported operand type(s) for -: 'int' and 'function'

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().

typeerror unsupported operand type for minus int and function

Here is an example of how the error occurs.

main.py
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.

# Call the function with parentheses to solve the error

To solve the error, make sure to call the function using parentheses.

main.py
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 can get its return value.

If your function takes any arguments, make sure to provide them when calling it, e.g. my_func(10, 20).
main.py
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.

# Checking the type of a variable

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
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.

# Table of Contents

  1. TypeError: unsupported operand type(s) for /: STR and INT
  2. Unsupported operand type(s) for *: FLOAT and decimal.Decimal
  3. TypeError: unsupported operand type(s) for /: tuple and int

# TypeError: unsupported operand type(s) for /: str and int

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.

typeerror unsupported operand type for slash str and int

Here is an example of how the error occurs.

main.py
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.

# Convert the string to a number before dividing

To solve the error, convert the string to a number before using the division operator.

main.py
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.

main.py
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.

main.py
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.

# Solve the error when using the input() function

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.

main.py
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.

main.py
# โœ… convert input to 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.

main.py
result = num_1 // num_2

# Checking what type a variable stores

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
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.

# Table of Contents

  1. Unsupported operand type(s) for *: FLOAT and decimal.Decimal
  2. TypeError: unsupported operand type(s) for /: tuple and int

# Unsupported operand type(s) for *: float and decimal.Decimal

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.

typeerror unsupported operand type for asterisk float and decimal decimal

Here is an example of how the error occurs.

main.py
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.

# Convert the floating-point number to a Decimal object

Since Decimal objects and floats are handled differently, we have to convert the floating-point number to a Decimal object.

main.py
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.

The value we pass to the 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.

# Checking what type a variable stores

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
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.

# TypeError: unsupported operand type(s) for /: tuple and int

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.

typeerror unsupported operand type for slash tuple and int

Here is an example of how the error occurs.

main.py
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.

To solve the error, you have to figure out how the variable got assigned a tuple object and correct the assignment.

# How tuples are constructed in Python

If you declared a tuple by mistake, tuples are constructed in the following ways:

  • Using a pair of parentheses () creates an empty tuple
  • Using a trailing comma - a, or (a,)
  • Separating items with commas - a, b or (a, b)
  • Using the tuple() constructor

# Divide each number in a tuple by a constant

If you need to iterate over the tuple and divide each number, use a for loop.

main.py
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.

main.py
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.

# Access a specific tuple element

If you need to use the division operator on an item in the tuple, access the item at its specific index.

main.py
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.

# Getting the sum of the tuple

If you need to get the sum of the tuple, use the sum() function.

main.py
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.

# Checking the type of the vairable

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev