sys.stdout.write
is (in Python 2) the only robust solution. Python 2 printing is insane. Consider this code:
print "a",
print "b",
This will print a b
, leading you to suspect that it is printing a trailing space. But this is not correct. Try this instead:
print "a",
sys.stdout.write("0")
print "b",
This will print a0b
. How do you explain that? Where have the spaces gone?
I still can't quite make out what's really going on here. Could somebody look over my best guess:
My attempt at deducing the rules when you have a trailing ,
on your print
:
First, let's assume that print ,
(in Python 2) doesn't print any whitespace (spaces nor newlines).
Python 2 does, however, pay attention to how you are printing - are you using print
, or sys.stdout.write
, or something else? If you make two consecutive calls to print
, then Python will insist on putting in a space in between the two.