Substituting variables in string with re

Robert Vollmert rvollmert at gmx.net
Wed Mar 1 18:24:29 CET 2000


Hi,

> Below is a (sligthly edited (it worked, all errors are due editing)) part
> of my code that substitutes variables (defined as ${variable}) in a string
> with a value from a dictionary:

you could simply use the %-operator to do this:

>>> 'insert %(variable)s here' % {'variable': 'value'}
'insert value here'
>>>

The extra 's' determines the formatting of the variable.

Apart from that, you might want to try to use the 'string' module
instead of regular expressions. Often, this will lead to faster (and
cleaner) code.

HTH,
Robert

-- 
Robert Vollmert                                      rvollmert at gmx.net




More information about the Python-list mailing list