gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:55:49 25 4
gpt4 key购买 nike

我正在尝试理解 python 中的 splat-operators。我有一个代码:

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 的序列,以及是否可以打印这个没有空格的单词。期望的输出:

stackoverflow

最佳答案

您得到该结果是因为 print 自动在传递的参数之间添加空格。您需要修改 sep 参数以防止插入 sep 空格:

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

关于python - 如何使用不带空格的 splat-operator (*) 打印列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58940470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com