sprintf behaviour

Anders Hammarquist iko at cd.chalmers.se
Sat Feb 22 17:44:10 CET 2003


In article <hcs1y20736c.fsf at tjatte.nada.kth.se>,
Björn Lindberg <d95-bli at nada.kth.se> wrote:
>In C, I can do the following to get positioned parameters to the *printf
>family of functions:

>    printf("-- %2$s -- %1$s --\n", a, b);

>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?

Yes, or sort of anyway (assuming you want to control the order that
the substitutions appear, rather than rearranging the order). The
python % formatter will take a dict instead of a tuple, and lets you
do:

print '-- %(second)s -- %(first)s --' % { 'first' : 'a', 'second' : 'b' }

/Anders

-- 
 -- Of course I'm crazy, but that doesn't mean I'm wrong.
Anders Hammarquist                                  | iko at cd.chalmers.se
Physics student, Chalmers University of Technology, | Hem: +46 31 88 48 50
G|teborg, Sweden.           RADIO: SM6XMM and N2JGL | Mob: +46 707 27 86 87




More information about the Python-list mailing list