How to Concatenate Tuples in Python?

Estimated read time 1 min read

To concatenate tuples in Python, you can use the + operator or the += operator. Here’s an example:

# Example tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)

# Concatenate using the + operator
concatenated_tuple = tuple1 + tuple2
print(concatenated_tuple)  # Output: (1, 2, 3, 4, 5, 6)

# Concatenate using the += operator
tuple1 += tuple2
print(tuple1)  # Output: (1, 2, 3, 4, 5, 6)

In this example, we have two tuples, tuple1 and tuple2, which contain some elements.

To concatenate the tuples:

  • Using the + operator: You can use the + operator to concatenate two tuples, such as tuple1 + tuple2. This creates a new tuple that contains all the elements from both tuples.
  • Using the += operator: The += operator allows you to concatenate a tuple to an existing tuple, modifying the original tuple. For example, tuple1 += tuple2 concatenates tuple2 to tuple1 and updates tuple1 with the concatenated result.

In both cases, the resulting tuple will contain all the elements from both tuples in the order they appear.

Note that tuples are immutable, which means they cannot be modified directly. So when you concatenate tuples, a new tuple is created with the concatenated elements.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply