sprintf behaviour

Björn Lindberg d95-bli at nada.kth.se
Sat Feb 22 11:32:50 EST 2003


"Andrew Wilkinson" <ajw126NO at SPAMyork.ac.uk> writes:

> >>> print "-- %2$s -- %1$s --" % ('a', 'b')
> 
> Python isn't C you know! You can't simply copy things from C to Python and
> expect them to work.

I know, but the tutorial says something to the effect that most C
format modifiers are supported in Python.

> Take a look at...
> http://www.python.org/doc/current/lib/typesseq-strings.html
> 
> And you'll see that what you actually want is just...
> >>> print "-- %s -- %s --" % ('a', 'b')

Actually, no. What I want to do, and what the C example does (sorry if
I was unclear), is to expand the second argument to the first
position, and vice versa.

>From the link you gave me, I can see how to do it in Python though:

>>> print "-- %(2)s -- %(1)s --" % {'1': 'a', '2' : 'b'}

Unfortunately, it doesn't work with sequences, only dictionaries, but
that is only a minor inconvenience.


Björn




More information about the Python-list mailing list