pets = ['boa', 'cat', 'dog']
for pet in pets:
print(pet)
boa
cat
dog
>>> for pet in pets:
print(pet, end=', ')
boa, cat, dog,
>>> for pet in pets:
print(pet, end='!!! ')
boa!!! cat!!! dog!!!
但是九月呢?我试图用 sep 替换 end 但什么也没发生,但我知道 sep 在打印时用于分隔,我如何以及何时可以使用 sep? sep 和 end 有什么区别?
原文由 MMM 发布,翻译遵循 CC BY-SA 4.0 许可协议
打印函数 使用
sep
分隔参数,并在最后一个参数之后使用end
。你的例子令人困惑,因为你只给了它一个论点。这个例子可能更清楚:当然,
sep
和end
仅适用于Python 3的打印功能。对于 Python 2,以下内容是等效的。您还可以在 Python 2 中使用向后移植版本的打印函数: