Skip to content Skip to sidebar Skip to footer

Python - Print A String A Certain Number Of Times

Possible Duplicate: Python, Printing multiple times, I'd like to know how to print a string such as 'String' 500 hundred times?

Solution 1:

You can use repetition (*):

print('String' * 500)

In this way Python will first create the whole string ("StringStr...String") in memory and only then will print it.

If you don't want to use so much memory, then you can use a for loop:

for i in range(500):
    print('String', end='')
print()

end='' is needed to prevent Python from printing end-of-line characters after each print. print() at the end is needed to print the end-of-line character after the last print.


Post a Comment for "Python - Print A String A Certain Number Of Times"