Python display string multiple times

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

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

print('-',3) 

.., which would mean typing - 3 times, for example:

 --- 
+70
python
Jun 08 '09 at 1:11
source share
5 answers

Python 2.x:

 print '-' * 3 

Python 3.x:

 print('-' * 3) 
+153
Jun 08 '09 at 1:13
source share

The accepted answer is short and nice, but there is an alternative syntax here that allows you to use a delimiter in Python 3.x.

 print(*3*('-',), sep='_') 
+1
Feb 09 '18 at 4:37
source share

To print a string 3 times in Python 3.x, in this case the string would be "hello"

print ("hello" * 3)

0
Feb 21 '15 at 23:41
source share
 st=input("Enter a string") n=input("No of times to repeat:") print(st * int(n)) 
0
Jun 01 '17 at 9:34 on
source share

Adding to all the correct answers above, in case our line is not static, we can use this as well:

 print(("%s" % "$")*3) 
0
Jul 11 '19 at 3:59
source share



All Articles