Python 使用 sys.stdout 打印尾随逗号行为

Python Print Trailing Comma Behavior using sys.stdout

在 Python (2.x) 中打印时,您可以添加尾随逗号来抑制换行符,如下所示:

print "This will be first ",
print "and this will be on the same line."

sys.stdout有没有办法做到这一点?类似于:

sys.stdout("This will be first ", please_dont=True)
sys.stdout("and this will be on the same line.")

sys.stdout.write 不会自动添加新行,您不需要额外的选项:

import sys

sys.stdout.write("This will be first ")
sys.stdout.write("and this will be on the same line.")

输出:This will be first and this will be on the same line.

试试这个:

sys.stdout.write("This will be first ") 
sys.stdout.write("and this will be on the same line.\n")   
sys.stdout.flush()