Join and repeat tuples using + & *
In Python, tuples are immutable sequences. You can still combine and repeat them easily using operators:
+ joins (concatenates) two or more tuples into a new tuple.* repeats the elements of a tuple multiple times.+ to join tuples end-to-end.* n to repeat the same tuple n times.t1 + t2 + t3.Concatenation syntax:
new_tuple = tuple_a + tuple_b # join two tuples into a new tuple
Repetition syntax:
repeated_tuple = some_tuple * n # repeat the same tuple n times
Both operations work because tuples support these arithmetic-like operators. The result is always a new tuple that contains elements from the original in order.
+
# join two number tuples and print the result
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
joined_tuple = tuple1 + tuple2
print(joined_tuple)
*
# repeat a tuple of fruits three times
tuple1 = ("apple", "banana")
repeated_tuple = tuple1 * 3
print(repeated_tuple)
# combine three smaller tuples into one larger tuple
tuple1 = (1, 2)
tuple2 = (3, 4)
tuple3 = (5, 6)
all_together = tuple1 + tuple2 + tuple3
print(all_together)
(1, 2, 3, 4, 5, 6)
tuple1 followed by all elements of tuple2.
('apple', 'banana', 'apple', 'banana', 'apple', 'banana')
("apple", "banana") is repeated 3 times.
(1, 2, 3, 4, 5, 6)
In each case, the original tuples (tuple1, tuple2, tuple3) remain unchanged; only the new tuples (joined_tuple, repeated_tuple, all_together) store the combined values.
+ to concatenate tuples into a single combined tuple.* when you need the same tuple elements repeated multiple times.*.