• Home
  • 4.9 String Repetition

4.9 String Repetition

View Categories

4.9 String Repetition

2 min read

String repetition is the process of repeating the contents of a string multiple times.

Python uses the * operator to repeat a string. The operator multiplies the string by an integer, producing a new string that contains the original string repeated the specified number of times.

String repetition is useful for creating separators, decorative patterns, repeated messages, padding, and simple text formatting.


Syntax #

string_name * number

Components #

Component Description
string_name The string to be repeated.
* Repetition operator.
number Number of times the string is repeated. Must be an integer.

Example 1: Repeating a String Three Times #

main.py

text = "Hi"

print(text * 3)

Output #

HiHiHi

Explanation #

  • The string "Hi" is repeated three times.
  • Python joins all repetitions into one new string.
  • The original string remains unchanged.

Example 2: Repeating a Character #

main.py

symbol = "*"

print(symbol * 10)

Output #

**********

Explanation #

  • The character * is repeated ten times.
  • This is commonly used to create separators or borders.

Example 3: Repeating a Word with Spaces #

main.py

word = "Python "

print(word * 4)

Output #

Python Python Python Python

Explanation #

  • The space is part of the string.
  • Therefore, each repetition includes the trailing space.
  • The words appear separated automatically.

Example 4: Repeating an Empty String #

main.py

text = ""

print(text * 5)

Output #

Explanation #

  • An empty string contains no characters.
  • Repeating it any number of times still produces an empty string.

Example 5: Using a Variable as the Repetition Count #

main.py

message = "Welcome! "
count = 3

print(message * count)

Output #

Welcome! Welcome! Welcome!

Explanation #

  • The repetition count can be stored in a variable.
  • Python repeats the string according to the variable’s value.

Example 6: User Input #

main.py

word = input("Enter a word: ")

print(word * 2)

Sample Input #

Hello

Output #

HelloHello

Explanation #

  • The user enters a string.
  • The string is repeated twice.
  • No space is added automatically.

Example 7: Repeating a Numeric String #

main.py

number = "123"

print(number * 3)

Output #

123123123

Explanation #

  • "123" is a string, not an integer.
  • Python repeats the characters instead of performing multiplication.

Example 8: Repeating Zero Times #

main.py

text = "Python"

print(text * 0)

Output #

Explanation #

  • Repeating a string zero times produces an empty string.
  • No error occurs.

Example 9: Building a Divider Line #

main.py

divider = "-"

print(divider * 20)

Output #

--------------------

Explanation #

  • The dash character is repeated twenty times.
  • This technique is commonly used to create divider lines.

Example 10: Checking the Result Type #

main.py

text = "Hi"

result = text * 5

print(result)
print(type(result))

Output #

HiHiHiHiHi
<class 'str'>

Explanation #

  • Repetition creates a new string.
  • The resulting object is still of type str.

Common Mistakes #

1. Using a Floating-Point Number #

Incorrect

text = "Python"

print(text * 2.5)

Output #

TypeError: can't multiply sequence by non-int of type 'float'

Reason

The repetition count must be an integer.

Correct

print(text * 2)

2. Expecting Spaces to Be Added Automatically #

Incorrect

text = "Hi"

print(text * 3)

Incorrect Expectation #

Hi Hi Hi

Actual Output #

HiHiHi

Reason

Python repeats the string exactly as written.

Correct

text = "Hi "

print(text * 3)

3. Using a Negative Repetition Count #

Incorrect

text = "Python"

print(text * -2)

Incorrect Expectation #

PythonPython

Actual Output #

Reason

A negative repetition count returns an empty string.


4. Confusing String Repetition with Numeric Multiplication #

Incorrect

print("10" * 3)

Incorrect Expectation #

30

Actual Output #

101010

Reason

"10" is a string.
The * operator repeats the string instead of multiplying numbers.

Correct

print(int("10") * 3)

Best Practices #

  • Use repetition to create separators and decorative text.
  • Remember that spaces are repeated if they are part of the string.
  • Use only integer values as the repetition count.
  • Convert numeric strings to integers if arithmetic multiplication is required.
  • Store the repeated string in a variable if it will be used multiple times.

Key Points to Remember #

  • The * operator repeats a string.
  • The repetition count must be an integer.
  • Repeating a string zero or a negative number of times returns an empty string.
  • Python repeats the string exactly as written.
  • String repetition creates a new string.
  • The original string remains unchanged.
  • The result of string repetition is always of type str.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *