TypeError: can only concatenate tuple (not "str") to tuple

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

TypeError: can only concatenate tuple (not "str") to tuple #

The Python "TypeError: can only concatenate tuple (not "str") to tuple" occurs when we try to concatenate a tuple and a string. To solve the error, make sure that the values are either 2 tuples or 2 strings before concatenating them.

typeerror can only concatenate list not nonetype to list

Here is an example of how the error occurs.

main.py
my_tuple = ('apple', 'banana') my_str = 'kiwi' # ⛔️ TypeError: can only concatenate tuple (not "str") to tuple result = my_tuple + my_str

We tried to use the addition (+) operator to concatenate a tuple and string which caused the error.

The values on the left and right-hand sides need to be of compatible types.

If you meant to concatenate two tuples, declare a tuple instead of a string.

main.py
my_tuple = ('apple', 'banana') my_other_tuple = ('kiwi',) result = my_tuple + my_other_tuple print(result) # 👉️ ('apple', 'banana', 'kiwi')

If you meant to concatenate 2 strings, make sure the two variables store a string.

Tuples are constructed in multiple 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

If you meant to access an element at a specific index in the tuple, use square brackets.

main.py
my_tuple = ('apple', 'banana') my_str = ' is a fruit' result = my_tuple[0] + my_str print(result) # 👉️ "apple is a fruit"

We concatenated the first element in the tuple to a string. Note that the values on both sides of the addition operator are strings, so this is allowed.

If you want to print the contents of a tuple, use a formatted string literal.

main.py
my_tuple = ('apple', 'banana') my_str = 'are fruits' result = f'{my_tuple} {my_str}' print(result) # 👉️ "('apple', 'banana') are fruits"
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

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

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

main.py
my_tuple = ('apple', 'banana') print(type(my_tuple)) # 👉️ <class 'tuple'> print(isinstance(my_tuple, tuple)) # 👉️ True my_str = 'are fruits' 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.

Conclusion #

The Python "TypeError: can only concatenate tuple (not "str") to tuple" occurs when we try to concatenate a tuple and a string. To solve the error, make sure that the values are either 2 tuples or 2 strings before concatenating them.

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.