percent string replacement with index

U

Ulrich Eckhardt

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
 
T

Terry Reedy

Ulrich said:
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:
'one is not two or zero. It is just one'
 
M

Matimus

Ulrich said:
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")
  "%[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")


Or even:
"{0[1]} is not {0[2]} or {0[0]}. It is just {0[1]}".format(["zero", "one", "two"])
'one is not two or zero. It is just one'

Or
'one is not two or zero. It is just one'

Or
... def __init__(self, zero, one, two):
... self.zero = zero
... self.one = one
... self.two = two
...'one is not two or zero. It is just one'

More information: http://www.python.org/dev/peps/pep-3101/

Exciting stuff.

Matt
 
U

Ulrich Eckhardt

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

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

Thanks Terry and Matimus, actually I'm using 2.4 and considering upgrading
now. ;)

Uli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top