percent string replacement with index

Ulrich Eckhardt eckhardt at satorlaser.com
Tue Jun 24 12:01:42 EDT 2008


Hi!

I'm still mostly learning Python and there is one thing that puzzles me
about string formatting. Typical string formatting has these syntaxes:

  "%s is %s" % ("GNU", "not Unix")
  "%(1)s %(2)s" % ("1":"one", "2":"two")

What I'm surprised is that this isn't supported:

  "%(1)s %(2)s" % ("zero", "one", "two")

i.e. specifying the index in a sequence instead of the key into a map (maybe
I would use [1] instead of (1) though). Further, the key can't be a simple
number it seems, which makes this even more inconvenient to me.

Can anyone explain this to me?

Also, why isn't the 's' conversion (i.e. to a string) the default? I
personally would like to just write something like this:

  "%1 is not %2" % ("zero", "one", "two")

or maybe

  "%[1] is not %[2]" % ("zero", "one", "two")


greetings!

Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list