percent string replacement with index

Terry Reedy tjreedy at udel.edu
Tue Jun 24 15:26:35 EDT 2008



Ulrich Eckhardt wrote:

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

History.  See below.
> 
> 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")

In 2.6 (I believe) and 3.0:

 >>> "{1} is not {2} or {0}. It is just {1}".format("zero", "one", "two")
'one is not two or zero. It is just one'




More information about the Python-list mailing list