在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.")
答案 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()