473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

percent string replacement with index

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

Jun 27 '08 #1
3 1832


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'

Jun 27 '08 #2
On Jun 24, 12:26*pm, Terry Reedy <tjre...@udel.eduwrote:
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")

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}".format(zero="zero", one="one", two="two")
'one is not two or zero. It is just one'

Or
>>class C(object):
... def __init__(self, zero, one, two):
... self.zero = zero
... self.one = one
... self.two = two
...
>>"{0.one} is not {0.two} or {0.zero}. It is just {0.one}".format(C("zero", "one", "two"))
'one is not two or zero. It is just one'

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

Exciting stuff.

Matt
Jun 27 '08 #3
Ulrich Eckhardt wrote:
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

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

Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: J Trost | last post by:
I was wondering if anyone knows if it is possible to do basic string replacement using XSLT even though the strings being replaced may contain "<" and ">". Here is my problem: I need to be able...
1
by: a_the_s | last post by:
Forethought: I'm using FreeBSD 5.1, gcc 3.2.1 The program takes a string (completely unaware of its contents), parses out HTML, then sends it on its way. Simply put - is there an {easy} way to...
4
by: andrewflanders | last post by:
I have an associative array of keys and values. I want to search a string for the existance of keys and replace them with the values in the array. The problem is that some of the keys resemble...
2
by: Art | last post by:
Hi & help, I'm trying to parse arithmetic expressions such as 4*(7-2.1). The first thing I'm trying is to add spaces to get: 4 * ( 7 - 2.1 ). I thought that Regular Expressions would be the...
4
by: Sjaakie | last post by:
I need to replace parts of a string, in a collection deserialized from an XML file, with values from another collection. Is there another, more clever/faster/better, method than the loops below? ...
2
by: SteveKlett | last post by:
<err, frustrating!> This code: var v = 'I am a string'; alert(v); alert(v); Works fine in Mozilla but not in IE. v in IE is 'undefined'
11
by: sam_cit | last post by:
Hi Everyone, I have the following question regarding string replacement pre-processor statement, #define status "23" #define status "44" int main() {
4
by: Phil Sandler | last post by:
Hello, What is the fastest/most efficient way of doing string replacement in csharp/.net? An example would be: "Hello, my name is {FirstName}, and I live in the town of {City} with my wife...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.