Interpolating Variables Inside Strings : Template « String « Python Tutorial






import string

values = [5, 3, 'blue', 'red']
s = string.Template("Variable v = $v")

for x in values:
    print s.substitute(v=x)








5.36.Template
5.36.1.template strings
5.36.2.Interpolating Variables Inside Strings
5.36.3.If the replacement field is part of a word, the name must be enclosed in braces
5.36.4.In order to insert a dollar sign, use $$
5.36.5.you can supply the value-name pairs in a dictionary
5.36.6.template systems (in this case using HTML):