Keyword substitution in string

Simon Dahlbacka simon.NO.dahlbacka.SPAM at abo.fi
Thu Aug 12 14:32:19 EDT 2004


"Ondrej Krajicek" <krajicek at ics.muni.cz> wrote in message
news:I2CH9G.I09 at 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"}

?





More information about the Python-list mailing list