我的功能定义是这样。
def repeat(s,n):
return s * n
>>> repeat('yeah',3)
'yeahyeahyeah'
当我调用函数repeat并输入我的整数要字符串重复多少次时,我希望它看起来像这样,包括空格和逗号。
>>> repeat('yeah,3)
'yeah, yeah, yeah'
最佳答案
使用str.join
例如:
def repeat(s,n):
return ", ".join([s] * n)
print(repeat('yeah',3))
输出:
yeah, yeah, yeah
注意:我也在列表中有参数(
s
)。
关于python - 如何在Python中返回以逗号和空格分隔的参数副本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50577704/