简体   繁体   中英

Print String multiple times on the same line

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?

if val > 0:
   print('%s ' % userString * val)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM