How to Add a String to a Tuple in Python

There are times when we need to modify tuples in Python, to add extra information or rearrange data. Tuples, however, are immutable data types in Python — once defined, they cannot be changed.

But don’t worry, there’s a workaround for that! You can’t technically add a string to a tuple, but we can concatenate the tuple with another one containing the desired string. This tutorial will take you through the procedure step-by-step.

Step 1: Set Up a Python Tuple and a String

Begin by setting up the originating tuple and the string you want to add:

Note: Items within a tuple are enclosed in round brackets and separated by commas.

Step 2: Add the String to the Tuple

As we can’t directly add a string to a tuple, we’ll convert the string to a single-item tuple:

We’ll then concatenate the original tuple with the new tuple to effectively add the string:

Step 3: Print the Final Tuple

Let’s print the final tuple to confirm that string addition was successful:

The output would look like this:

'item1', 'item2', 'item3', 'new_item'

Full Code

We’ve used a step-by-step approach for clarity. The full concise code for adding a string to a tuple in Python is as follows:

Conclusion

The process of adding a string to a Python tuple may seem complex due to the tuple’s immutable nature, but it’s entirely manageable through concatenation. While we technically create a new tuple rather than modifying the existing one, the end result meets our needs.

Have a look at Python’s official Tuples and Sequences documentation for more on dealing with Python tuples.