使用打印函数的“sep”参数打印由破折号分隔的“*”字符

Cod*_*ody 2 python python-3.x

我正在学习 Python,只是想弄清楚如何打印"*"由破折号分隔的多个字符,但用户可以更改星号和破折号的数量。

这是我到目前为止:

print('*' * n, sep = '-' * m)
Run Code Online (Sandbox Code Playgroud)

其中 n 和 m 是整数。但它并没有真正起作用。

我想要的结果(如果 n = 3 且 m = 2)是:

*--*--*
Run Code Online (Sandbox Code Playgroud)

Ash*_*ary 5

>>> print(*['*']*n, sep='-'*m)
*--*--*
Run Code Online (Sandbox Code Playgroud)

对于单个字符,这也适用:

>>> print(*('*'*n), sep='-'*m) #this can fail if you use `'**'` instead of `'*'`
*--*--*
Run Code Online (Sandbox Code Playgroud)

['*']*n在此处创建一个列表,现在我们将此列表解压缩为print()using*'-'*m用作sep.:

>>> ['*']*n
['*', '*', '*']
Run Code Online (Sandbox Code Playgroud)