How to stop print printing spaces?

CC crobc at BOGUS.sbcglobal.net
Sun Jul 29 00:03:16 CEST 2007


Hi:

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]) )

print


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


Thanks for input.




-- 
_____________________
Christopher R. Carlen
crobc at bogus-remove-me.sbcglobal.net
SuSE 9.1 Linux 2.6.5



More information about the Python-list mailing list