Keyword substitution in string

Tim Jarman tmj at jarmania.com
Thu Aug 12 14:31:00 EDT 2004


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




More information about the Python-list mailing list