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?

2
  • are you using python 3.X ? Commented Nov 9, 2014 at 18:50
  • 1
    I am using 2.X @Kasra Commented Nov 9, 2014 at 18:53

2 Answers 2

2
if val > 0:
   print('%s ' % userString * val)
Sign up to request clarification or add additional context in comments.

Comments

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 ""
***** ***** ***** ***** *****

3 Comments

print(userString * val), this prints 123 X times on the same line and X new lines
@user2955610, yes either will work but if using the import it must be the very first line in your file
@PadraicCunningham i have a question, i use mostly python 2x, i know in future 2x will be deprecated, you have any advice for me??. as python 3x is future

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.