sprintf behaviour

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


In C, I can do the following to get positioned parameters to the *printf
family of functions:

#include <stdio.h>

int main()
{
    char * a = "abc";
    char * b = "def";
    printf("-- %2$s -- %1$s --\n", a, b);
    return 0;
}

Unfortunately, this does not seem to work in Python:

>>> print "-- %2$s -- %1$s --" % ('a', 'b')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: unsupported format character '$' (0x24) at index 5
>>> 

Why is this? Is it a bug or an oversight? I'm running Python 2.3a1 on
GNU/Linux. Is there some other way to achieve what I want?


Björn




More information about the Python-list mailing list