sprintf behaviour

Björn Lindberg d95-bli at nada.kth.se
Sat Feb 22 18:08:50 CET 2003


Carl Banks <imbosol-1045931606 at aerojockey.com> writes:

> Also, the 1$ thing in the printf is nonstandard.  I guess it works
> with GCC, but I doubt it works with other C compilers.  Even if Python
> did try to duplicate printf exactly, it wouldn't be a bug to leave out
> a nonstandard feature.

AFAIK, it is included in POSIX, so it should work on any
POSIX-compatible system.

> Yes.  What you need to do is define a function such as byindex below;
> this takes a tuple and turns it into a dict where the keys are the
> stringized tuple indices:
> 
>     def byindex(t):
>         n = len(t)
>         r = map(str,xrange(n))
>         return dict(zip(r,t))
> 
> Because the Python % operator can accept a dict, you can use this to
> get the effect you seek, like this:
> 
>     >>> print "--- %(1)s --- %(0)s ---" % byindex(('a', 'b'))

Thanks for the nice solution.


Björn




More information about the Python-list mailing list