Re: Controlling output using print with format string



On Sun, 30 Oct 2005 18:44:06 -0600, Paul Watson <pwatson@xxxxxxxxxxxxx> 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
.



Relevant Pages

  • ApostaC - Variadic functions RFCAS
    ... Variadic functions in C (the ... ... printf looks just like C's printf. ... ApostaC's printf uses the flexability of the format string, ... How do other languages support variadic functions? ...
    (comp.programming)
  • Re: ApostaC - Variadic functions RFCAS
    ... but at a cost of losing type safety where it is arguably most ... > printf looks just like C's printf. ... > localisation by swapping the format string. ... > How do other languages support variadic functions? ...
    (comp.programming)
  • printf behaviour with illegal or malformed format string
    ... Obligatory bikeshed avoidance notice: ... Given that illegal or malformed format strings to the printf family ... a bogus format string will result in abort. ...
    (freebsd-arch)
  • Re: printf behaviour with illegal or malformed format string
    ... > Given that illegal or malformed format strings to the printf family ... and therefore the question gains a bit more relevance. ... > If the variable is set, a bogus format string will result in abort. ... Many other platforms allow it for 64bit architectures as well. ...
    (freebsd-arch)
  • Re: printing
    ... the reason i was originally wanting the printf capability ... is because i'm converting a program which uses them. ... It is not always a literal format string that ... > no particular advantage to imitating C syntax here. ...
    (comp.lang.lisp)