*
Multiplication for numbers, repetition for sequences — "ab" * 3 is 'ababab'.
Common call
6 * 7
Returns
product, or repeated sequence
Replaces
[0] * 5 builds a 5-zero list
Watch out
[[0]] * 3 repeats the SAME inner list — aliasing trap
aa — Left operand.type: number | sequence · required * bb — Right operand — the repeat count when a is a sequence.type: number | int · required
→ number | sequence
Demo
Live evaluation
Try:
Inputs
afloatleft operand
bfloatright operand
Output
6 * 7
42
Numeric multiplication here; in real Python the same symbol repeats sequences ("ab" * 3, [0] * 5) when one operand is an int.
Operands
| Name | Type | Required | Description |
|---|---|---|---|
| a | number | sequence | yes | Left operand. |
| b | number | int | yes | Right operand — the repeat count when a is a sequence. |
Return value
number | sequence — The product of two numbers — or a sequence repeated int times.
Common patterns
Initialize a flat list
Safe for immutable fillers like numbers or None.
zeros = [0] * width
Separator lines
String repetition for quick formatting.
print("-" * 40)
2-D grids — the safe way
A comprehension makes each row distinct; * would alias them.
grid = [[0] * w for _ in range(h)]
Examples
1. Numbers
6 * 7
Returns
422. String repetition
"ab" * 3
Returns
'ababab'3. List repetition
[0] * 4
Returns
[0, 0, 0, 0]4. Zero repeats
"x" * 0
Returns
''Pitfalls
1. Nested list repetition aliases
* copies REFERENCES — all three inner lists are one object.
Aliased rows
g = [[0]] * 3 g[0].append(1) g
[[0, 1], [0, 1], [0, 1]]
Fix
g = [[0] for _ in range(3)] g[0].append(1) g
[[0, 1], [0], [0]]
2. Float repeat counts raise
Sequence repetition needs an int.
Raises
"ab" * 2.0
TypeError: can't multiply sequence by non-int of type 'float'
Fix
"ab" * int(2.0)
'abab'
When to use
Use it
- Numeric products
- Repeating strings for formatting
- Flat lists of an immutable filler
Reach for something else
- Nested/mutable fillers → list comprehension
- Products of an iterable → math.prod
- Matrix multiplication → the @ operator (numpy)
Notes
Complexity
O(1) numbers; O(len·count) sequences
Return
new value; operands untouched
CPython impl
Objects/abstract.c :: PyNumber_Multiply → __mul__ / __rmul__
Memory
Repetition allocates the full result
Thread-safe
Yes — operands are not mutated
FAQ
Repetition copies references, not objects. Three slots point at one list. Use a comprehension to build distinct inner lists.
History
3.5
Related: @ (matrix multiplication) added as a separate operator.
1.0
Core operator from the beginning.