0

Python String Operation (string duplcation)

print("spam" * 3) output; spamspamspam print(4 * '2') output: 2222 print("pythonisfun" * 2) output:pythonisfunpythonisfun Is there anyway for me to put a space between the output, as if I want to use this syntax to make a duplicated number of words as in: print("python is fun" * 2) in order for me to see: python is fun python is fun

9th Dec 2018, 9:49 AM
Gary Jovan
Gary Jovan - avatar
4 Answers
+ 5
ShortCode solution let a trailing space at end of the generated string (or at start if you add the space at start before multiplying the string to be repeated)... if you want to avoid that, you could trim the generated string, or you could do: print(' '.join(['spam']*3))
9th Dec 2018, 11:13 AM
visph
visph - avatar
+ 3
That's what's done by both previous solutions ^^ ShortCode solution add a space to the original string before joining n repeatitions, while mine build a list with n elements before joining them together using a space separator ;)
9th Dec 2018, 11:59 AM
visph
visph - avatar
+ 1
"python is fun " * 2
9th Dec 2018, 10:24 AM
ShortCode
0
thank you for your help, but I am trying to give the output the seperation (spacing) instead of joining them together @visph
9th Dec 2018, 11:54 AM
Gary Jovan
Gary Jovan - avatar