How to add a space when you're multiplying variables in python

丶灬走出姿态 提交于 2020-12-26 12:15:28

问题


Here is my code

print("Type in another set of words")
    x = input()
    print("Now type in how many times you want it to appear (WHOLE NUMBERS ONLY!)")
    c = int(input())
    print(x * c)

I want to add a space between every time the word is multiplied. Is that possible?


回答1:


Use str.join to place a space between a sequence of strings:

print(' '.join([x] * c))



回答2:


You can use string formatting:

print('{} '.format(x)*c)


来源:https://stackoverflow.com/questions/18938965/how-to-add-a-space-when-youre-multiplying-variables-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!