Welcome, guest | Sign In | My Account | Store | Cart

New to python 2.4 is the String.Template class. It is similar to the formatting python already had in which the % operator with strings was used in a recognizable sprintf style of formatting from C. In addition to format types, you also had a choice with a tuple or dictionary mapping. String.Template simplifies it to only the string format type and dictionary mapping. This makes string formatting easier to remember and use. This example shows you the basics of using the template and the difference with the old style.

Python, 40 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import string

#With the new style, you mark a label in a string with with a $ and use a #dictionary to map that label to something else. 

new_style=string.Template('this is $thing')
print new_style%{'thing':5}      #produces:  this is 5                              
print new_style%{'thing':'test'} #produces: this is test

#With the old style you have to specify a type as a formatting code
#in this case, %s is used for a string format. Note how the old style 
#string is not as clean looking.

old_style='this is %(thing)s'
print old_style%{'thing':5}      #produces: this is 5
print old_style%{'thing':'test'} #produces: this is test

#Here is an example that shows 3 basic uses of the new Template.
#The standard mapping with $customer, handeling of the case where characters #not part of the label directly follow it with ${name}Inn, and finally the use #of $$ to insert an actual $ into the string.

form_letter='''Dear $customer,
I hope you are having a great time.
If you do not find Room $room to your satisfaction,
let us know. Please accept this $$5 coupon.
            Sincerely,
            $manager
            ${name}Inn'''
                                        
letter_template=string.Template(form_letter)

print letter_template%{'name':'Sleepy','customer':'Fred Smith',
                       'manager':'Barney Mills','room':307,
                      }
#produces:
Dear Fred Smith,
I hope you are having a great time.
If you do not find Room 307 to your satisfaction,
let us know. Please accept this $5 coupon.
            Sincerely,
            Barney Mills
            SleepyInn

The string.Template is a welcome addition to string formatting. The older sprintf style can be tedious to remember formatting for the dictionary mapping if you do not use it often. And, if you are familar with languages like perl, string.Template will also look familar since substitution in strings in perl use the $ notation.

There is also a SafeTemplate class you can use in cases where the dictionary may not have all the labels. And finally, in true python style, you can of course subclass String.Template to do more fancy things.

1 comment

John Nielsen (author) 19 years, 7 months ago  # | flag

oops, previous posting seemed to fail sorry for the duplicate. When I tried posting the first the the web browser said it failed. I simply resubmitted it. I should have checked to see if it actually _did_ fail.

Created by John Nielsen on Thu, 9 Sep 2004 (PSF)
Python recipes (4591)
John Nielsen's recipes (36)

Required Modules

  • (none specified)

Other Information and Tasks