473,378 Members | 1,139 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,378 software developers and data experts.

Keyword substitution in string


Hello,

in my application, I want to do keyword substituion in
a string from a dictionary. The problem is indeed
easy to solve, but seems quite common to me, so I wonder
wheter there is already a function in Python which
does just that.

I want to substitute values for keywords in strings,
the keywords and values are stored in a dictionary.

Something like this:

def subst(string, dict):
...

where:

subst('Hello, <key1> <key2>!', {'key1': 'Python', 'key2': 'rules' })

gives:

'Hello, Python rules!'

The keyword quoting style does not matter.

Thanks,

Ondra

--
____________________________\\--//_________________________
Ondrej Krajicek \\// kr******@ics.muni.cz
Institute of Computer Science,||Masaryk University Brno, CR
_____________________________//\\__________________________
Jul 18 '05 #1
3 6361

On 12 Aug 2004, at 18:43, Ondrej Krajicek wrote:

Hello,

in my application, I want to do keyword substituion in
a string from a dictionary. The problem is indeed
easy to solve, but seems quite common to me, so I wonder
wheter there is already a function in Python which
does just that.

I want to substitute values for keywords in strings,
the keywords and values are stored in a dictionary.

Something like this:

def subst(string, dict):
...

where:

subst('Hello, <key1> <key2>!', {'key1': 'Python', 'key2': 'rules' })

gives:

'Hello, Python rules!'

The keyword quoting style does not matter.

Thanks,

Ondra


Yes - so common they already thought of it! See Library Reference
2.3.6.1 or here: http://docs.python.org/lib/typesseq-strings.html for
an example of exactly what you want.

HTH

Tim J

Jul 18 '05 #2
On Thu, 12 Aug 2004, Ondrej Krajicek wrote:
in my application, I want to do keyword substituion in
a string from a dictionary. The problem is indeed
easy to solve, but seems quite common to me, so I wonder
wheter there is already a function in Python which
does just that.
Indeed there is:
subst('Hello, <key1> <key2>!', {'key1': 'Python', 'key2': 'rules' })

gives:

'Hello, Python rules!'


'Hello, %(key1)s %(key2)s!' % {'key1': 'Python', 'key2': 'rules'}

gives:

'Hello, Python rules!'

:)

Aside from the (key) after the %, this works just like normal %
substitution.

Jul 18 '05 #3

"Ondrej Krajicek" <kr******@ics.muni.cz> wrote in message
news:I2********@news.muni.cz...

Hello,

in my application, I want to do keyword substituion in
a string from a dictionary. The problem is indeed
easy to solve, but seems quite common to me, so I wonder
wheter there is already a function in Python which
does just that.

I want to substitute values for keywords in strings,
the keywords and values are stored in a dictionary.

Something like this:

def subst(string, dict):
...

where:

subst('Hello, <key1> <key2>!', {'key1': 'Python', 'key2': 'rules' })

gives:

'Hello, Python rules!'

The keyword quoting style does not matter.


Are you aware of:

"Hello %(key1)s %(key2)s!" % {"key1":"Python", "key2":"rules"}

?
Jul 18 '05 #4

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

Similar topics

20
by: Grumble | last post by:
Hello everyone, As far as I understand, the 'inline' keyword is a hint for the compiler to consider the function in question as a candidate for inlining, yes? What happens when a function with...
1
by: kollareddy | last post by:
Hi all, I am new to xml/xsd world. I want to know the differences between complex type and element being abstract and if both can be declared so, in case of substitution goups.Also can xsi:type...
1
by: Rodolfo | last post by:
Hello, there's another languages that can do a macro substitution, how can I do this in Csharp. This is an example of what I want to do Dataset ds = new Dataset; string a = "ds"; DataSet...
4
by: Don | last post by:
I think "macro substitution" is the correct term for what I want to do, but, to be sure, here is a description of what I'd like to know is possible: I want to be able to create a create an object...
5
by: Murali | last post by:
In Python, dictionaries can have any hashable value as a string. In particular I can say d = {} d = "Right" d = "Wrong" d = "test" In order to print "test" using % substitution I can say
4
by: Ian | last post by:
Hi, Hopefully a simple question but my brain is hurting... I want to make a regex substitution, using search and replace patterns contained in variables. What I want to do is: $f =...
2
by: Steve | last post by:
Hi, I'm currently teaching myself about XML schems at the same time as specifying the XML document for a project I've been given to write. (I'm new to the XML world, so progress is a little slow...
8
by: Laser Lu | last post by:
Sometimes, I need to do some time-consuming operations based on whether a specific keyword was contained in a lengthy string. Then, for a better performance, I wrapped that lengthy string into a...
6
by: Generic Usenet Account | last post by:
I was extremely surprised to learn that the extremely rich C++ string API does not have even a single menthod devoted to string substitution i.e. given a string, replace all instances of pattern-1...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.