此代码来自 http://docs.python.org/2/tutorial/errors.html#predefined-clean-up-actions
with open("myfile.txt") as f:
for line in f:
print line,
我不明白打印命令末尾的 ,
是什么。
我还检查了文档,http://docs.python.org/2/library/functions.html#print .
理解不够,是不是搞错了?(好像不是,来自官方教程)。
我来自 ruby/javascript,这对我来说很不寻常。
最佳答案
在python 2.7中,逗号是为了表示字符串会打印在同一行
例如:
for i in xrange(10):
print i,
这将打印出来
1 2 3 4 5 6 7 8 9
要在 python 3 中执行此操作,您可以这样做:
for i in xrange(10):
print(i,end=" ")
你可能会发现这个答案很有帮助
Printing horizontally in python
---- 编辑 ---
文档,http://docs.python.org/2/reference/simple_stmts.html#the-print-statement , 说
A '\n' character is written at the end, unless the print statement ends with a comma.
关于python - 打印功能中的逗号结尾有什么用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18908897/