Python - 打印一定数量的字符串

Thi*_* G. 0 python

可能重复:
Python,多次打印,

我想知道如何打印诸如"String"之类的字符串500次?

cit*_*txx 15

你可以使用重复(*):

print('String' * 500)
Run Code Online (Sandbox Code Playgroud)

通过这种方式,Python将首先"StringStr...String"在内存中创建整个string(),然后才会打印它.

如果你不想使用这么多内存,那么你可以使用for循环:

for i in range(500):
    print('String', end='')
print()
Run Code Online (Sandbox Code Playgroud)

end=''需要防止Python在每个字符后打印行尾字符print.print()最后需要在最后一行之后打印行尾字符print.