← Back to Chapters

Python Tuple Join

? Python Tuple Join

Join and repeat tuples using + & *

? Quick Overview

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.
  • Original tuples are never modified; new tuples are created.

? Key Concepts

  • Tuple concatenation: use + to join tuples end-to-end.
  • Tuple repetition: use * n to repeat the same tuple n times.
  • Immutability: operations return new tuples instead of changing existing ones.
  • Multiple joins: you can join many tuples in a single expression, like t1 + t2 + t3.
  • Mixed data: tuples can store numbers, strings, booleans, etc., and you can still join them.

? Syntax & Theory

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.

? Code Examples

➕ Join two tuples with +
# 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 with *
# repeat a tuple of fruits three times
tuple1 = ("apple", "banana")
repeated_tuple = tuple1 * 3
print(repeated_tuple)
? Join multiple tuples together
# 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)

? Live Output & Explanation

?️ What the code prints

  • For the join example:
    (1, 2, 3, 4, 5, 6)
    The new tuple contains all elements of tuple1 followed by all elements of tuple2.
  • For the repetition example:
    ('apple', 'banana', 'apple', 'banana', 'apple', 'banana')
    The original tuple ("apple", "banana") is repeated 3 times.
  • For joining multiple tuples:
    (1, 2, 3, 4, 5, 6)
    All elements from the three tuples are combined in order.

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.

? Use Cases

  • Merging configuration values from different sources into a single tuple.
  • Repeating a pattern of values when generating test data.
  • Building longer sequences step by step by joining tuples from different parts of a program.

? Tips & Best Practices

  • Use + to concatenate tuples into a single combined tuple.
  • Use * when you need the same tuple elements repeated multiple times.
  • Remember that joining tuples does not modify the originals; it always creates a new tuple.
  • Feel free to store mixed data types in tuples; joining still works correctly.
  • If you need to modify elements frequently, consider using a list instead of a tuple.

? Try It Yourself

  • Create two tuples of fruits and join them, then print the result.
  • Make a tuple of numbers and repeat it 3 times using *.
  • Join three tuples with different data types (numbers, strings, booleans) and inspect the result.
  • Join an empty tuple with a non-empty tuple and see what changes (or doesn’t!).
  • Experiment by using a large repetition number (like 10) and check the length of the result.