Skip to content

Computer Science · Ch 10 — Tuples and Dictionaries

Repetition

10.2.2

Repetition

The repetition operation is written with the * symbol and repeats the elements of a tuple a given number of times. The repetition operator has a strict requirement on its operands: the first operand must be a tuple and the second operand must be an integer — nothing else.

>>> greeting = ('Hi', 'There')
>>> greeting * 3
('Hi', 'There', 'Hi', 'There', 'Hi', 'There')

Repetition works on a single-element tuple as well (again note the trailing comma that makes it a tuple in the first place):

>>> beat = ('Dhol',)      # tuple with a single element
>>> beat * 4
('Dhol', 'Dhol', 'Dhol', 'Dhol')
``` …