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
"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.
- 1Identify the original string
The original string is "go".
- 2Identify the multiplier
The multiplier is 4, so the string is repeated four times.
- 3Concatenate 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 |
String replication creates a new string by repeating text, while numeric multiplication produces a number.
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- 1After the first line, symbol contains "=".
- 2After the second line, count contains 4.
- 3After the third line, line contains "====".
- symbol receives the string "=".
- count receives the integer 4.
- line receives the new string formed from four copies of "=".
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 |
Zero and negative integers return an empty string; a float is not a valid multiplier for string replication.
Mistakes Beginners Make
Common mistake Expecting string replication to add the string and the number together
What it looks like"ha" * 3 produces "ha3"Why it failsThe integer is a repetition count, not text that is appended to the string.
Do this insteadRead the integer as the number of copies: "ha" * 3 produces "hahaha".
Common mistake Expecting a zero multiplier to preserve the original string
What it looks like"ready" * 0Why it failsRepeating a string zero times creates no copies.
Do this insteadTreat the result as an empty string.
Common mistake Expecting a negative multiplier to create a reverse or negative version of the string
What it looks like"abc" * -1Why it failsNegative repetition is not meaningful for string replication.
Do this insteadRemember that a negative integer returns an empty string, just like zero.
Common mistake Using a float as the multiplier
What it looks like"ab" * 2.5Why it failsThe multiplier for string replication must be an integer.
Do this insteadUse 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
- 1The * operator repeats a string when the other operand is an integer.
- 2The integer specifies how many times the original string is concatenated.
- 3Multiplying a string by zero or a negative integer returns an empty string.
- 4Using a float as the multiplier raises a
TypeError. - 5Tracing the string value and multiplier separately helps debug replication expressions.
At a glance
- 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.