Formatting Strings for Output
The count applies to the whole string on the other side of the star operator. If the original string contains several characters, every repetition includes all of those characters in the same order.
A Shortcut for Repeated Text
When output needs the same text several times, the star operator provides a compact way to create it. Multiplying a string by an integer produces a new string containing the original string concatenated the specified number of times. This is useful for visual separators, repeated patterns, and formatted output.
A border made from five asterisks can be created by repeating the string containing one asterisk five times. The resulting text is five asterisks in a row, which can be used above or below a message.
From One Copy to Many
String replication can be understood as repeated concatenation. First, Python starts with the original string. It then places another copy directly after it for each additional repetition required by the integer. The result is a new string; the original string is the material being copied into that result.
Three Copies of a String
Determine the result of repeating the string ha three times.
- 1Start with the original
The original string is ha.
- 2Use the count
The integer is 3, so three copies are required.
- 3Concatenate the copies
Place the copies directly next to one another: ha, then ha, then ha.
The resulting string is hahaha.
Reading the Repetition Count
The integer determines how many copies appear in the result. A larger positive integer produces more copies, while a smaller positive integer produces fewer copies. To predict an expression, identify the original string, identify the integer, and count that many copies without adding extra spaces or characters.
What do you think happens?
Predict the result of repeating the string = three times.
Reveal answer
===
The original string contains one equals sign, and the positive integer specifies three copies.
The count applies to the whole string on the other side of the star operator. If the original string contains several characters, every repetition includes all of those characters in the same order.
Zero and Negative Counts
A repetition count of zero produces an empty string because the original text is requested zero times. A negative integer also produces an empty string. Negative repetition is not meaningful, so it is treated the same as zero repetition for string replication.
| Repetition count | Result |
|---|---|
| Positive integer | The original string appears that many times |
| 0 | An empty string |
| Negative integer | An empty string |
| Float | A TypeError is raised |
Effects of different multiplier values in string replication
Tracing a Formatting Problem
When debugging string replication, trace the expression from left to right. First record the string being repeated. Next record the multiplier and check whether it is an integer. Then determine how many copies should appear. Finally, inspect the resulting string, including whether it is empty and whether any spaces belong to the original string.
Tracing a Border
A border is created by repeating the asterisk string five times, and a message is placed between two borders.
- 1Identify the string
The string being repeated is one asterisk.
- 2Identify the multiplier
The multiplier is the integer 5, so the result must contain five copies.
- 3Compute the value
The border variable holds five asterisks in a row.
- 4Check the formatted output
The message is printed between the upper and lower border strings.
The border value is *****.
Common mistake Expecting a negative count to create a reversed or negative-length string.
What it looks likeA negative repetition count produces an empty string.
Why it failsNegative repetition is not meaningful for string replication.
Do this insteadTreat zero and negative counts as cases that produce no characters.
Common mistake Using a floating-point multiplier.
What it looks likeA repetition count such as 2.5 is not an integer.
Why it failsThe multiplier must be an integer, and a float raises a
TypeError.Do this insteadCheck that the repetition count is an integer before tracing the result.
Common mistake Counting only one character when the original string contains several characters.
What it looks likeRepeating ha three times produces hahaha, not haaah.
Why it failsEach repetition includes the entire original string.
Do this insteadCopy the complete original string once for each count.
Practice the Prediction
For each expression, predict the resulting string before checking an answer: repeat the string xy two times; repeat the string - three times; repeat the string = zero times; then decide what happens when the multiplier is 1.5.
Hints
- Keep the complete original string together for every copy.
- Zero and negative integer counts produce an empty string.
- A floating-point multiplier raises a
TypeError.
- 1Write down the original string exactly, including spaces and punctuation.
- 2Check whether the multiplier is an integer.
- 3For a positive integer, count that many complete copies.
- 4For zero or a negative integer, record an empty string.
- 5For a float, record that a
TypeErroris raised.
Key Takeaways
- 1The star operator repeats a string when it is multiplied by an integer.
- 2A positive integer determines the number of complete copies in the new string.
- 3Zero and negative integers both produce an empty string.
- 4A floating-point multiplier raises a
TypeError. - 5Tracing the original string, multiplier type, count, and resulting value makes replication problems easier to debug.
At a glance
- String replication creates a new string by concatenating the original string the requested number of times.
- Positive integer counts produce repeated copies, while zero and negative counts produce an empty string.
- The multiplier must be an integer; floats raise a
TypeError. - Visual separators, patterns, and formatted output are common uses for string replication.
- Debugging is easier when you trace the original string, multiplier type, repetition count, and final value in order.