Display string multiple times

前端 未结 2 1396
别跟我提以往
别跟我提以往 2020-11-29 03:19

I want to print a character or string like \'-\' n number of times.

Can I do it without using a loop?.. Is there a function like

print(\'-\',3)


        
相关标签:
2条回答
  • 2020-11-29 03:41

    Python 2.x:

    print '-' * 3
    

    Python 3.x:

    print('-' * 3)
    
    0 讨论(0)
  • 2020-11-29 03:53

    The accepted answer is short and sweet, but here is an alternate syntax allowing to provide a separator in Python 3.x.

    print(*3*('-',), sep='_')
    
    0 讨论(0)
提交回复
热议问题