1

I am trying to print a string, multiple times together on the same line.

For example: User input = 123 and I need to print it 3 times: 123123123

This is my code that i have tried:

userString = []

    if val > 0:
    for i in range(val):
        print(userString * val, end = " ")

it's giving me a syntax error by the end=""

How can I fix this?

Pj_
  • 824
  • 6
  • 15
user2955610
  • 815
  • 2
  • 9
  • 15

2 Answers2

2
if val > 0:
   print('%s ' % userString * val)
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
0

You are using python 2 not python 3 , you can either use:

from __future__ import print_function # import the print function 

Or use:

print(userString * val), # <- trailing comma 

You could also use join and a list comprehension to match the output of your python 3 print code:

val = 5
print(" ".join(["*" * val for _ in range(val)])) if val else ""
***** ***** ***** ***** *****
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321