Print a string multiple times in Python

avatar

Borislav Hadzhiev

Last updated: Sep 3, 2022

banner

Photo from Unsplash

Print a string multiple times in Python #

To print a string multiple times:

  1. Use the multiplication operator to repeat the string N times.
  2. Use the print() function to print the result.
  3. The print() function will print the string repeated the specified number of times.
main.py
# ✅ Print a string multiple times on the same line my_str = 'z' print(my_str * 4) # 👉️ 'zzzz' # ----------------------------------------- # ✅ Print an integer multiple times my_int = 9 print(str(my_int) * 4) # 👉️ '9999' # ----------------------------------------- # ✅ Print string multiple times (using f-string) result = f'Result: {my_str * 4}' print(result) # 👉️ Result: zzzz # ----------------------------------------- # ✅ Print string multiple times on separate lines for i in range(3): print(my_str) # ✅ Print string multiple times on separate lines (f-string) result = f'{my_str}\n' * 3 print(result, end='')

We used the multiplication operator to print a string multiple times.

When used with a string and an integer, the multiplication operator repeats the string the specified number of times.
main.py
my_str = 'z' print(my_str * 4) # 👉️ 'zzzz' print(my_str * 3) # 👉️ 'zzz'

This prints the string the specified number of times on the same line.

If you need to print an integer multiple times, make sure to convert it to a string before using the multiplication operator.

main.py
my_int = 9 print(str(my_int) * 4) # 👉️ '9999'
We used the str() class to convert the integer to a string before multiplying it by 4.

You can also use a formatted string literal to print a string multiple times.

main.py
result = f'Result: {my_str * 4}' print(result) # 👉️ Result: zzzz

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

If you need to print a string multiple times on separate lines, append an \n character to the string before using the multiplication operator.

main.py
my_str = 'z' # 'z' # 'z' # 'z' result = f'{my_str}\n' * 3 print(result, end='')

Notice that we added a newline (\n) character at the end of the string.

We set the end keyword argument to an empty string to remove the last newline character.

The end argument is printed at the end of the message. By default, end is set to a newline character (\n).

main.py
print('a', 'b', 'c') # 👉️ 'a b c\n' print('a', 'b', 'c', end='') # 👉️ 'a b c'

We already added a newline character at the end of the string, so an extra newline isn't necessary.

Another commonly used argument the print() function takes is sep.

The sep argument is the separator between the arguments we pass to print().

By default, the argument is set to a space.

main.py
print('a', 'b', 'c') # 👉️ 'a b c' print('a', 'b', 'c', sep='') # 👉️ 'abc'

You can also use a for loop with the range() class to print a string multiple times on separate lines.

main.py
my_str = 'z' # 'z' # 'z' # 'z' for i in range(3): print(my_str)

We used the range class to loop 3 times and used the print() function to print the string on each iteration.

The range class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you only pass a single argument to the range() constructor, it is considered to be the value for the stop parameter.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.