如何使用不带空格的 splat-operator (*) 打印列表

How to print a list using splat-operator (*) without spaces

我正在尝试了解 python 中的 splat 运算符。我有一个代码:

word = ['s', 't', 'a', 'c', 'k', 'o', 'v', 'e', 'r', 'f', 'l', 'o', 'w']
print(*word)

输出:

s t a c k o v e r f l o w

我无法将 *word 分配给某个变量以检查其类型或调试中的其他内容。所以我想知道 print() 以何种方式获得 *word 的序列,以及是否可以在没有空格的情况下打印这个词。理想的输出:

Whosebug

你可以传递你想要的分隔符,在你的情况下:

print(*word, sep="")

或者做:

print(''.join(word))

你得到那个结果是因为 print 自动在传递的参数之间放置空格。您需要修改 sep 参数以防止插入 sep 注释空间:

print(*word, sep="")  # No implicit separation space
Whosebug