How to print a string multiple times in python?

Strings are a set of characters enclosed within a single or double quote. Consider a string "Red ", printing the string 3 times will give output as "Red Red Red". This article discusses different methods to print a string multiple times in python.

Printing a string multiple times using the * operator

The multiplication operator (*) prints a string multiple times in the same line. Multiplying a string with an integer n concatenates the string with itself n times.

Example,

print("Python"*3)
print("Python "*3)

Output

PythonPythonPython
Python Python Python 

To print the strings multiple times in a new line, we can append the string with the newline character '\n'. For example, the following code prints the string "Python" in a new line.

print("Python\n"*3)

Output

Python
Python
Python

Printing a string multiple times using the for loop

A for loop iterates over a range and in each iteration it prints a string. The print() function by default prints the string in a newline. To print the string in the same line we can pass the end parameter(end = " ") to the print() function.

print("Print string in the same line: ")
for i in range(3):
      print("Programming", end =" ")
print()  # printing newline
print("Print string in the newline: ")
for i in range(3):
     print("Programming")

Output

Print string in the same line: 
Programming Programming Programming 
Print string in the newline: 
Programming
Programming
Programming

Printing a string multiple times using the format() function

In python 3 the format() function formats string in a more efficient way. string.format() replaces the placeholders with the specified strings present inside the format() function. The placeholder is represented with a pair of curly braces {}. A string can be printed multiple times by creating multiple placeholders.

Consider a string "Python". The string is passed as a parameter to the format() function. All the placeholders are assigned to an index value of 0 so that all the placeholders are replaced with the same string.

print("Print string in the same line: ")
print("{0}{0}{0}{0}".format("Python "))
print("Print string in the newline: ")
print("{0}{0}{0}{0}".format("Python\n"))

Output

Print string in the same line: 
Python Python Python Python 
Print string in the newline: 
Python
Python
Python
Python
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions