Concepts / Working with String Variables

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.

  • Programming

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?

  • "hahaha"

  • "ha3"

  • "hhh"

  • An error

Reveal answer
Answer:

"hahaha"

The original string is concatenated three times: "ha" followed by "ha" followed by "ha".

ha1 copyhaha2 copieshahaha3 copiesadd one copyadd one copy
How does the original string become a longer string when it is multiplied by an integer?

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.

python
Output
*****
Worked example

Counting Copies in a String

Determine the value of result after result = "go" * 4.

  1. 1
    Identify the original string

    The original string is "go".

  2. 2
    Identify the multiplier

    The multiplier is 4, so the string is repeated four times.

  3. 3
    Concatenate the copies

    Place the copies directly next to one another: "go" + "go" + "go" + "go".

Result

result contains "gogogogo".

ExpressionOperandsResult
"go" * 4String and integer"gogogogo"
4 * 3Numbers12

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.

Code walkthrough
symbol = "="
count = 4
line = symbol * count
  1. 1
    After the first line, symbol contains "=".
  2. 2
    After the second line, count contains 4.
  3. 3
    After the third line, line contains "====".
  • symbol receives the string "=".
  • count receives the integer 4.
  • line receives the new string formed from four copies of "=".
symbol"="count4* operatorrepeat 4 timesline"===="provides stringprovides integercreates new string
What is the value of the string and multiplication count at each step as the expression executes?
Debugging

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.

MultiplierMeaningResult for "ab"
3Repeat three times"ababab"
0Repeat zero times""
-2Negative repetition""
2.5Not an integerTypeError

Zero and negative integers return an empty string; a float is not a valid multiplier for string replication.

3"ababab"0""-2""count decreasescount becomes negative
What changes in the output when the string is multiplied by zero or by a negative integer?

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 fails

    The integer is a repetition count, not text that is appended to the string.

    Do this instead

    Read 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" * 0
    Why it fails

    Repeating a string zero times creates no copies.

    Do this instead

    Treat 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" * -1
    Why it fails

    Negative repetition is not meaningful for string replication.

    Do this instead

    Remember 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.5
    Why it fails

    The multiplier for string replication must be an integer.

    Do this instead

    Use an integer count when applying the * operator to a string.

Best practice

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

EASY

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.
In the real world

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

  1. 1
    The * operator repeats a string when the other operand is an integer.
  2. 2
    The integer specifies how many times the original string is concatenated.
  3. 3
    Multiplying a string by zero or a negative integer returns an empty string.
  4. 4
    Using a float as the multiplier raises a TypeError.
  5. 5
    Tracing 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.

Related concepts

More concepts starting with W