How to stop print printing spaces?

CC crobc at
Sun Jul 29 00:03:16 CEST 2007


I've conjured up the idea of building a hex line editor as a first real 
Python programming exercise.

To begin figuring out how to display a line of data as two-digit hex 
bytes, I created a hunk of data then printed it:

ln = '\x00\x01\xFF 456789abcdef'
for i in range(0,15):
     print '%.2X ' % ord(ln[i]),

This prints:
00  01  FF  20  34  35  36  37  38  39  61  62  63  64  65

because print adds a space after each invocation.

I only want one space, which I get if I omit the space in the format 
string above.  But this is annoying, since print is doing more than what 
I tell it to do.

What if I wanted no spaces at all?  Then I'd have to do something 
obnoxious like:

for i in range(0,15):
     print '\x08%.2X' % ord(ln[i]),

This works:

import sys
for i in range(0,15):
     sys.stdout.write( '%.2X' % ord(ln[i]) )


Is that the best way, to work directly on the stdout stream?

Thanks for input.

Christopher R. Carlen
crobc at
SuSE 9.1 Linux 2.6.5

More information about the Python-list mailing list