Keyword substitution in string

Christopher T King squirrel at WPI.EDU
Thu Aug 12 14:31:06 EDT 2004


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.




More information about the Python-list mailing list