Keyword substitution in string

O

Ondrej Krajicek

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
 
T

Tim Jarman

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
 
C

Christopher T King

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.
 
S

Simon Dahlbacka

Ondrej Krajicek said:
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"}

?
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top