How to stop print printing spaces?

C

CC

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

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

This works:

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

print


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


Thanks for input.
 
R

Roel Schroeven

CC schreef:
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),

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

This works:

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

print


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


It's not a bad idea: print is mostly designed to be used in interactive
mode and for quick and dirty logging; not really for normal program
output (though I often use it for that purpose).

There is another way: construct the full output string before printing
it. You can do that efficiently using a list comprehension and the
string method join():
>>> print ''.join(['%.2X' % ord(c) for c in ln])
0001FF20343536373839616263646566

In Python 2.4 and higher, you can use a generator expression instead of
a list comprehension:
0001FF20343536373839616263646566

BTW, in your examples it's more pythonic not to use range with an index
variable in the for-loop; you can loop directly over the contents of ln
like this:

import sys
for c in ln:
sys.stdout.write('%.2X' % ord(c))
print
 
C

CC

Roel said:
CC schreef:
ln = '\x00\x01\xFF 456789abcdef'
# This works:
import sys
for i in range(0,15):
sys.stdout.write( '%.2X' % ord(ln) )
print
Is that the best way, to work directly on the stdout stream?


It's not a bad idea: print is mostly designed to be used in interactive
mode and for quick and dirty logging; not really for normal program
output (though I often use it for that purpose).

There is another way: construct the full output string before printing
it. You can do that efficiently using a list comprehension and the
string method join():
print ''.join(['%.2X' % ord(c) for c in ln])
0001FF20343536373839616263646566


Oh yeah, that's right!
In Python 2.4 and higher, you can use a generator expression instead of
a list comprehension:

0001FF20343536373839616263646566

Hmm. I'm at 2.3 :-(
BTW, in your examples it's more pythonic not to use range with an index
variable in the for-loop; you can loop directly over the contents of ln
like this:

import sys
for c in ln:
sys.stdout.write('%.2X' % ord(c))
print


Ah yes, duh! I played with this means of iterating over items in a
sequence the other day while studying the tutorial, but it sinks in
slowly for a C programmer.

That's why I posted my silly code. I'll probably do that a lot until I
get used to this pythonic business.

Thanks for the input!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top