By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

Controlling output using print with format string

P: n/a
It is clear that just using 'print' with variable names is relatively
uncontrollable. However, I thought that using a format string would
reign the problem in and give the desired output.

Must I resort to sys.stdout.write() to control output?

$ python
Python 2.4.1 (#1, Jul 19 2005, 14:16:43)
[GCC 4.0.0 20050519 (Red Hat 4.0.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
s = 'now is the time'
for c in s: .... print c,
....
n o w i s t h e t i m e for c in s: .... print "%c" % (c),
....
n o w i s t h e t i m e for c in s:

.... print "%1c" % (c),
....
n o w i s t h e t i m e
Oct 31 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Paul Watson <pw*****@redlinepy.com> wrote:
It is clear that just using 'print' with variable names is relatively
uncontrollable. However, I thought that using a format string would
reign the problem in and give the desired output.

Must I resort to sys.stdout.write() to control output?


In general, yes, because print tries hard to separate with spaces the
things it is separately printing. You can fight that by resetting
sys.stdout.softspace after each and every print, but that's harder than
just using sys.stdout.write. So, you should use the right tool for the
job: print for simple output, often diagnostic in nature, where you
don't mind the spaces and/or newlines that implies; sys.stdout.write
when you do want to really control every aspect of the output.
Alex
Oct 31 '05 #2

P: n/a
Paul Watson <pw*****@redlinepy.com> writes:
It is clear that just using 'print' with variable names is relatively
uncontrollable. However, I thought that using a format string would
reign the problem in and give the desired output.

Must I resort to sys.stdout.write() to control output?
No. You can control the output of print to whatever degree you want -
just convert the printed objects to strings by hand before printing
them.
$ python
Python 2.4.1 (#1, Jul 19 2005, 14:16:43)
[GCC 4.0.0 20050519 (Red Hat 4.0.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = 'now is the time'
>>> for c in s: ... print c,
...
n o w i s t h e t i m e >>> for c in s: ... print "%c" % (c),
...
n o w i s t h e t i m e >>> for c in s:

... print "%1c" % (c),
...
n o w i s t h e t i m e


On the other hand, you can't keep print from putting a space between
all the objects it prints. So you have to convert all the objects into
a single string before printing that string.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 31 '05 #3

P: n/a
On Sun, 30 Oct 2005 18:44:06 -0600, Paul Watson <pw*****@redlinepy.com> wrote:
It is clear that just using 'print' with variable names is relatively
uncontrollable. However, I thought that using a format string would
reign the problem in and give the desired output.

Must I resort to sys.stdout.write() to control output?
Maybe. But I wouldn't say "uncontrollable" -- print is pretty predictable.
$ python
Python 2.4.1 (#1, Jul 19 2005, 14:16:43)
[GCC 4.0.0 20050519 (Red Hat 4.0.0-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
s = 'now is the time'
for c in s:... print c,
...
n o w i s t h e t i m e for c in s:... print "%c" % (c),
...
n o w i s t h e t i m e for c in s:... print "%1c" % (c),
...
n o w i s t h e t i m e


If you like C, you can make something pretty close to printf:
import sys
def printf(fmt, *args): ... s = fmt%args
... sys.stdout.write(s)
... return len(s)
... s = 'now is the time'
for c in s: ... printf('%s', c)
...
n1
o1
w1
1
i1
s1
1
t1
h1
e1
1
t1
i1
m1
e1

Oops, interactively you probably want to do something other
than implicity print the printf return value ;-)
s = 'now is the time'
for c in s: ... nc = printf('%s', c)
...
now is the time>>> for c in s: ... nc = printf('%c', c)
...
now is the time>>> for c in s: ... nc = printf('%1c', c)
...
now is the time>>>

Just to show multiple args, you could pass all the characters
separately, but at once, e.g., (of course you need a format to match)
printf('%s'*len(s)+'\n', *s) now is the time
16
printf('%s .. %s .. %s .. %s\n', *s.split())

now .. is .. the .. time
25

Or just don't return anything (None by default) from printf if you
just want to use it interactively. Whatever.

Your character-by-character output loop doesn't give much of a clue
to what obstacle you are really encountering ;-)

Regards,
Bengt Richter
Oct 31 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.