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 TypeErrors in Python, go to the articles:

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!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨