Using print with format to stdout generates unwanted space

Paul Watson pwatson at
Mon Jun 20 05:57:13 CEST 2005

#!/usr/bin/env python

#   Using a print statement to stdout results in an
#   unwanted space character being generated at the
#   end of each print output.  Same results on
#   DOS/Windows and AIX.
#   I need precise control over the bytes that are
#   produced.  Why is print doing this?
import sys

#   If this is a DOS/Windows platform, then put stdout
#   into binary mode so that only the UNIX compatible newline
#   will be generated.
    import msvcrt, os
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    print 'This is not an msvcrt platform.'

#   Using print with newline suppressed generates a space at the
#   end of each print statement.
for i in range(3):
    print '%d,60,' % (i),
    for j in range(10):
        print '%d,' % (j),
    print ''

#   Using a list and doing a join does not result in the space
#   character being generated.
for i in range(3):
    alist = []
    alist.append('%d,60,' % (i))
    for j in range(10):
        alist.append('%d,' % (j))
    print ''.join(alist)


More information about the Python-list mailing list