How to Solve Python TypeError: can only concatenate tuple (not “str”) to tuple

by | Programming, Python, Tips

This error occurs when we try to concatenate a tuple with a string. You can solve the error by ensuring that you concatenate either two tuples or two strings. For example,

tup = ('hexagon', 'pentagon', 'rhomboid')

tup2 = ('octagon',)

combined = tup + tup2

print(combined)

You can check the type of an object using the built-in type function.

This tutorial will go through the error in detail and how to solve it with code examples.


Python TypeError: can only concatenate tuple (not “str”) to tuple

TypeError tells us that we are trying to perform an illegal operation for a specific Python data type. We can only concatenate objects of the same type.

Example

Let’s look at an example to reproduce the error:

# Define a tuple

tup = ('hexagon', 'pentagon', 'rhomboid')

# Define a string

my_str = 'octagon'

# Concatenate a tuple with a string

combined = tup + my_str

# Print result to console

print(combined)

Let’s run the code to see what happens:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [6], in <cell line: 5>()
      1 tup = ('hexagon', 'pentagon', 'rhomboid')
      3 my_str = 'octagon'
----> 5 combined = tup + my_str
      7 print(combined)

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

The error occurs because we can only concatenate tuples with tuples or strings with strings. We can check the types of objects using the built-in type() function. For example,

print(type(my_str))
print(type(tup))
<class 'str'>
<class 'tuple'>

Solution #1

We can solve the error by converting the string to a tuple before concatenating. We can define a tuple with a single element by putting parentheses around the string and a comma after the element. Let’s look at the revised code:

# Define a tuple

tup = ('hexagon', 'pentagon', 'rhomboid')

# Convert string to tuple

tup2 = ('octagon',)

# Print types to confirm the objects are both tuples

print(type(tup))

print(type(tup2))

# Concatenate two tuples

combined = tup + tup2

# Print result to console

print(combined)

Let’s run the code to see the result:

<class 'tuple'>
<class 'tuple'>
('hexagon', 'pentagon', 'rhomboid', 'octagon')

We can also define a tuple but just putting a comma after the string. Let’s look at the revised code.

# Define a tuple

tup = ('hexagon', 'pentagon', 'rhomboid')

# Convert string to tuple

tup2 = 'octagon',

# Print types to confirm the objects are both tuples

print(type(tup))

print(type(tup2))

# Concatenate two tuples

combined = tup + tup2

# Print result to console

print(combined)

Let’s run the code to see the result:

<class 'tuple'>
<class 'tuple'>
('hexagon', 'pentagon', 'rhomboid', 'octagon')

Solution #2

We can also solve the error by accessing the individual strings in the tuple using the subscript operator and then performing the concatenation. Let’s look at the revised code:

tup = ('hexagon', 'pentagon', 'rhomboid')

my_str = 'octagon'

combined = tup[0] +', '+ tup[1] + ', ' +tup[2] + ', '+ my_str

print(combined)

Let’s run the code to see the concatenated string.

hexagon, pentagon, rhomboid, octagon

Summary

Congratulations on reading to the end of this tutorial!

For further reading on concatenation in Python, go to the article:

Python TypeError: can only concatenate str (not “int”) to str Solution

Go to the online courses page on Python to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Research Scientist at Moogsoft | + posts

Suf is a research scientist at Moogsoft, specializing in Natural Language Processing and Complex Networks. Previously he was a Postdoctoral Research Fellow in Data Science working on adaptations of cutting-edge physics analysis techniques to data-intensive problems in industry. In another life, he was an experimental particle physicist working on the ATLAS Experiment of the Large Hadron Collider. His passion is to share his experience as an academic moving into industry while continuing to pursue research. Find out more about the creator of the Research Scientist Pod here and sign up to the mailing list here!