Substitution with Hash Values

Jeff Shannon jeff at ccvcorp.com
Fri Apr 19 18:22:12 EDT 2002


In article <pan.2002.04.19.16.48.06.120768.1546 at jonesj.com>, 
jeff at jonesj.com says...

> I use html templates for all of my CGI programming, and everything that needs
> to be replaced is contained within double curly braces ( {{value}} ).  In
> perl I use a hash to keep all of the variables that are going to replace
> the placeholders in the template....

Well, I'm not familiar with Perl, but perhaps this will help you?

>>> mydict = { 'name': 'Jeff', 'food': 'Spam' }
>>> template = 'Hi, my name is %(name)s, and my favorite food is 
%(food)s!'
>>> print template % mydict
Hi, my name is Jeff, and my favorite food is Spam!
>>> 

Your template contains '%(var)s' as placeholders.  When you 
provide the string formatting operator (%) with a dictionary, it 
will then go through your template and substitute every '%(var)s' 
with the value of mydict[var].  

-- 

Jeff Shannon
Technician/Programmer
Credit International



More information about the Python-list mailing list