Formatting Strings for Output
The * operator repeats a string by multiplying it by an integer, producing a new string with the original string concatenated the specified number of times.
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.
Start with the original: The original string is ha.
Use the count: The integer is 3, so three copies are required.
Concatenate 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
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.
Identify the string: The string being repeated is one asterisk.
Identify the multiplier: The multiplier is the integer 5, so the result must contain five copies.
Compute the value: The border variable holds five asterisks in a row.
Check the formatted output: The message is printed between the upper and lower border strings.
The border value is *****.
Expecting a negative count to create a reversed or negative-length string.
Negative repetition is not meaningful for string replication.
Fix:
Treat zero and negative counts as cases that produce no characters.Using a floating-point multiplier.
The multiplier must be an integer, and a float raises a TypeError.
Fix:
Check that the repetition count is an integer before tracing the result.Counting only one character when the original string contains several characters.
Each repetition includes the entire original string.
Fix:
Copy 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.
- Write down the original string exactly, including spaces and punctuation.
- Check whether the multiplier is an integer.
- For a positive integer, count that many complete copies.
- For zero or a negative integer, record an empty string.
- For a float, record that a TypeError is raised.
Key Takeaways
- The star operator repeats a string when it is multiplied by an integer.
- A positive integer determines the number of complete copies in the new string.
- Zero and negative integers both produce an empty string.
- A floating-point multiplier raises a TypeError.
- Tracing the original string, multiplier type, count, and resulting value makes replication problems easier to debug.
Key Takeaways
- 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.