How do you concatenate two tuples in Python?

How do you concatenate two tuples in Python?

In Python, you can concatenate two tuples using the + operator or the += augmented assignment operator. Here's how you can do it:

Using the + operator:

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

concatenated_tuple = tuple1 + tuple2

print(concatenated_tuple)  # Output: (1, 2, 3, 4, 5, 6)

Using the += operator:

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

tuple1 += tuple2

print(tuple1)  # Output: (1, 2, 3, 4, 5, 6)

Both methods produce a new tuple that contains all the elements from both tuples. It's important to note that tuples are immutable, so the original tuples remain unchanged, and a new tuple is created with the concatenated result.