使用sys.stdout的Python Print Trailing Comma Behavior

时间:2015-09-16 15:27:23

标签: python 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.")

2 个答案:

答案 0 :(得分:3)

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.

答案 1 :(得分:1)

试试这个:

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