Python String Multiplication: Syntax and Behavior
Learn how Python string multiplication works with the * operator, including syntax, edge cases, practical use cases, and memory behavior.

String multiplication in Python uses the * operator to repeat a string: "ab" * 3 evaluates to "ababab". The syntax is minimal, but the behavior has edge cases that matter in real code. The operator is implemented by str.__mul__, and the reverse form 3 * "ab" works identically through str.__rmul__, so operand order does not affect the result.
prefix = ">> "
print(prefix * 3) # >> >> >>The repetition count must be an integer. A float such as 2.0 raises TypeError; the result is always a new string and the original is never modified.
What String Multiplication Does in Python
The * operator on a string repeats that string a given number of times, producing a single concatenated result. This is distinct from list multiplication, where [0] * 3 creates a list of three references to the same object. For strings, the semantics are simpler because strings are immutable and the result is always a new string value.
pattern = "ab" * 4
print(pattern) # ababababIn CPython, the operation is implemented natively, so the runtime can compute the required buffer size up front and allocate the result in one pass. For large values of n, this is much more efficient than building the same string through repeated concatenation in a loop.
How the Repetition Operator Behaves
The behavior of * on strings has specific edge cases that developers frequently overlook:
"ab" * 0returns""(the empty string)"ab" * -3also returns""(negative counts produce an empty string)"ab" * 2.0raisesTypeError, even though 2.0 is numerically integral
print(repr("ab" * 0)) # ''
print(repr("ab" * -3)) # ''The empty result for zero or negative counts is consistent with Python's sequence repetition semantics. If your code must reject negative counts, check the value explicitly before multiplying, because the operator itself will not raise an error for them.
Practical Use Cases for Repeating Strings
String multiplication is most useful where a fixed pattern must appear a known number of times:
- Visual separators in CLI output:
"-" * 40 - Indentation or padding:
" " * indent_level - Building fixed-width fields in text output
- Generating test fixtures with repeated patterns
def print_section(title: str, width: int = 60) -> None:
print(title)
print("=" * width)These cases share a common shape: the repetition count is known before the operation, and the result is consumed as a single string. When the count is dynamic but bounded, * remains the clearest expression of intent.
String Multiplication vs. Other Repetition Approaches
For repeating a single string, * is the natural tool. str.join is designed for joining a sequence of distinct strings, and using it to repeat one string requires constructing an intermediate list, which is both slower and less readable.
# Direct and clear
result = "ab" * 3
# Indirect and unnecessary
result = "".join(["ab"] * 3)itertools.repeat combined with join avoids writing a list literal, but join still has to collect the items before it can build the result. It adds an import and is rarely faster in practice for small counts. The table below summarizes the tradeoffs.
| Approach | Best for | Tradeoff |
|---|---|---|
"s" * n | Repeating one string | Concise, fast, single allocation |
"".join(["s"] * n) | Repeating one string using join | Requires an explicit list |
"".join(repeat("s", n)) | Avoiding a list literal in source | Less readable; join still collects items internally |
Choose * unless you are already joining a heterogeneous sequence of strings, in which case join is the correct tool regardless.
Memory and Performance Implications
String multiplication allocates the full result in one operation. For large counts this matters: "x" * 10**8 creates a 100 MB string in a single allocation. The allocation is efficient because CPython computes the size up front, but the memory footprint is real and immediate.
If you build a large string incrementally in a loop with +=, each step allocates a new string and copies the previous content, yielding O(n²) behavior in CPython. Multiplication avoids that by producing the final string directly. When the result must be assembled from repeated pieces, prefer * or a single join over a concatenation loop.
Common Mistakes and Misconceptions
A frequent mistake is using a float count. "ab" * 2.0 raises TypeError, even though 2.0 is mathematically an integer. If the count comes from a calculation that may produce a float, convert with int() first and validate the value.
Another misconception is that string multiplication mutates the original string. Strings are immutable, and each multiplication returns a new object. The original remains unchanged, which is usually what you want but is worth remembering when the source string is large and the operation is repeated.
There is also confusion between string and list multiplication. [0] * 3 creates a list of three references to the same integer object, which is harmless for immutable elements but problematic for mutable ones like lists or dicts. The syntax looks similar to string multiplication, but the semantics differ because lists are mutable containers.
When to Avoid String Multiplication
Avoid * when the count is unknown and potentially huge, because the result is materialized fully in memory. For streaming output, write chunks directly to the sink instead of building one giant string first.
# Avoid: materializes a 100 MB string
payload = "x" * 10**8
write(payload)
# Prefer: stream in chunks when the sink supports it
for _ in range(10**6):
write("x" * 100)Also avoid * when the count is derived from user input without validation. A very large count can cause a MemoryError or exhaust available memory. Validate or cap the count before multiplying, and consider whether the repeated content can be generated lazily instead of stored as one contiguous string.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.