Working with String Variables
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.
From One String to Many
A string can be repeated by using the * operator with an integer. The result is a new string in which the original string has been concatenated the specified number of times. Before reading the explanation, predict what the repeated string will look like when the original value is made of one character and when it contains several characters.
What do you think happens?
What is the result of repeating the string "ha" three times?
Reveal answer
Answer: "hahaha"
The original string is concatenated three times: "ha" followed by "ha" followed by "ha".
Reading the Replication Expression
In a string replication expression, one operand is the string and the other operand is an integer. The integer specifies how many times the string is repeated. The operation produces a new string; it does not produce a number. For example, multiplying a string containing an asterisk by 5 produces five asterisks in one string.
*****Counting Copies in a String
Determine the value of result after result = "go" * 4.
Identify the original string: The original string is "go".
Identify the multiplier: The multiplier is 4, so the string is repeated four times.
Concatenate the copies: Place the copies directly next to one another: "go" + "go" + "go" + "go".
result contains "gogogogo".
| Expression | Operands | Result |
|---|---|---|
| "go" * 4 | String and integer | "gogogogo" |
| 4 * 3 | Numbers | 12 |
Tracing Values During Execution
Debugging string replication becomes easier when you separate the expression into its parts. First identify the string value. Then identify the integer multiplier. Finally determine the new string formed by concatenating that many copies. This tracing process follows the source guidance to watch the variables being assigned and then see how the * operator transforms the string.
symbol = "=" count = 4 line = symbol * count
If a replicated string is not what you expected, inspect the string variable and the multiplier separately. A count of 5 applied to "*" creates "*****". A count of 10 creates ten asterisks. If the entire message is multiplied instead, the complete message is repeated rather than only its border.
Zero, Negative, and Invalid Counts
A string multiplied by zero produces an empty string because the original text is requested zero times. A string multiplied by a negative integer also produces an empty string. Negative repetition is not meaningful, so Python treats it the same as zero repetition.
| Multiplier | Meaning | Result for "ab" |
|---|---|---|
| 3 | Repeat three times | "ababab" |
| 0 | Repeat zero times | "" |
| -2 | Negative repetition | "" |
| 2.5 | Not an integer | TypeError |
Mistakes Beginners Make
Expecting string replication to add the string and the number together
The integer is a repetition count, not text that is appended to the string.
Fix:
Read the integer as the number of copies: "ha" * 3 produces "hahaha".Expecting a zero multiplier to preserve the original string
Repeating a string zero times creates no copies.
Fix:
Treat the result as an empty string.Expecting a negative multiplier to create a reverse or negative version of the string
Negative repetition is not meaningful for string replication.
Fix:
Remember that a negative integer returns an empty string, just like zero.Using a float as the multiplier
The multiplier for string replication must be an integer.
Fix:
Use an integer count when applying the * operator to a string.
When debugging, write down the string operand and the multiplier before predicting the result. This prevents you from confusing numeric multiplication with string replication and makes zero, negative, and invalid counts easier to identify.
Practice and Application
Predict the result of each expression before checking your reasoning: "-" * 6, "-" * 0, "-" * -3, and "OK" * 2. Then explain which expression would raise a TypeError if its multiplier were changed to 1.5.
Hints
- Count the copies for the positive integer.
- Zero and negative integer counts produce an empty string.
- A float is not a valid multiplier for string replication.
String replication is useful for visual separators, repeated characters, patterns, and formatted output. A border made from repeated asterisks is one example. For more complex repetition patterns or when fine control over spacing and formatting is needed, loops or string formatting methods may be more appropriate.
Key Takeaways
- The * operator repeats a string when the other operand is an integer.
- The integer specifies how many times the original string is concatenated.
- Multiplying a string by zero or a negative integer returns an empty string.
- Using a float as the multiplier raises a TypeError.
- Tracing the string value and multiplier separately helps debug replication expressions.
Key Takeaways
- Use a string and an integer with the * operator to create a repeated string.
- Predict the result by concatenating the original string the specified number of times.
- Zero and negative integers produce an empty string.
- A float multiplier raises a TypeError.
- Trace the string operand and count separately when debugging.