If a trailing comma is added to the end of a print statement, the next statement is executed first. Why is this? For example, this executes 10000 ** 10000 before it prints "Hi ":
print "Hi",
print 10000 ** 10000
And this takes a while before printing “Hi Hello”:
def sayHello():
for i in [0] * 100000000: pass
print "Hello"
print "Hi",
sayHello()
In Python 2.x, a trailing
,in aprintstatement prevents a new line to be emitted.print("Hi", end="")to achieve the same effect.The standard output is line-buffered. So the
"Hi"won’t be printed before a new line is emitted.