Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="whatsnew24.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="whatsnew24.html" title='What's New in Python 2.4' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='last' href='about.html' title='About this document...' /> | |
10 | <link rel='help' href='about.html' title='About this document...' /> | |
11 | <link rel="next" href="node6.html" /> | |
12 | <link rel="prev" href="node4.html" /> | |
13 | <link rel="parent" href="whatsnew24.html" /> | |
14 | <link rel="next" href="node6.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>4 PEP 292: Simpler String Substitutions</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="3 PEP 289: Generator" | |
24 | href="node4.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="What's New in Python" | |
27 | href="whatsnew24.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="5 PEP 318: Decorators" | |
30 | href="node6.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">What's New in Python 2.4</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><img src='../icons/blank.png' | |
37 | border='0' height='32' alt='' width='32' /></td> | |
38 | <td class='online-navigation'><img src='../icons/blank.png' | |
39 | border='0' height='32' alt='' width='32' /></td> | |
40 | </tr></table> | |
41 | <div class='online-navigation'> | |
42 | <b class="navlabel">Previous:</b> | |
43 | <a class="sectref" rel="prev" href="node4.html">3 PEP 289: Generator</A> | |
44 | <b class="navlabel">Up:</b> | |
45 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> | |
46 | <b class="navlabel">Next:</b> | |
47 | <a class="sectref" rel="next" href="node6.html">5 PEP 318: Decorators</A> | |
48 | </div> | |
49 | <hr /></div> | |
50 | </DIV> | |
51 | <!--End of Navigation Panel--> | |
52 | ||
53 | <H1><A NAME="SECTION000500000000000000000"> | |
54 | 4 PEP 292: Simpler String Substitutions</A> | |
55 | </H1> | |
56 | ||
57 | <P> | |
58 | Some new classes in the standard library provide an alternative | |
59 | mechanism for substituting variables into strings; this style of | |
60 | substitution may be better for applications where untrained | |
61 | users need to edit templates. | |
62 | ||
63 | <P> | |
64 | The usual way of substituting variables by name is the <code>%</code> | |
65 | operator: | |
66 | ||
67 | <P> | |
68 | <div class="verbatim"><pre> | |
69 | >>> '%(page)i: %(title)s' % {'page':2, 'title': 'The Best of Times'} | |
70 | '2: The Best of Times' | |
71 | </pre></div> | |
72 | ||
73 | <P> | |
74 | When writing the template string, it can be easy to forget the | |
75 | "<tt class="samp">i</tt>" or "<tt class="samp">s</tt>" after the closing parenthesis. This isn't a big | |
76 | problem if the template is in a Python module, because you run the | |
77 | code, get an ``Unsupported format character'' <tt class="exception">ValueError</tt>, | |
78 | and fix the problem. However, consider an application such as Mailman | |
79 | where template strings or translations are being edited by users who | |
80 | aren't aware of the Python language. The format string's syntax is | |
81 | complicated to explain to such users, and if they make a mistake, it's | |
82 | difficult to provide helpful feedback to them. | |
83 | ||
84 | <P> | |
85 | PEP 292 adds a <tt class="class">Template</tt> class to the <tt class="module">string</tt> module | |
86 | that uses "<tt class="samp">$</tt>" to indicate a substitution. <tt class="class">Template</tt> is a | |
87 | subclass of the built-in Unicode type, so the result is always a | |
88 | Unicode string: | |
89 | ||
90 | <P> | |
91 | <div class="verbatim"><pre> | |
92 | >>> import string | |
93 | >>> t = string.Template('$page: $title') | |
94 | >>> t.substitute({'page':2, 'title': 'The Best of Times'}) | |
95 | u'2: The Best of Times' | |
96 | </pre></div> | |
97 | ||
98 | <P> | |
99 | If a key is missing from the dictionary, the <tt class="method">substitute</tt> method | |
100 | will raise a <tt class="exception">KeyError</tt>. There's also a <tt class="method">safe_substitute</tt> | |
101 | method that ignores missing keys: | |
102 | ||
103 | <P> | |
104 | <div class="verbatim"><pre> | |
105 | >>> t = string.SafeTemplate('$page: $title') | |
106 | >>> t.safe_substitute({'page':3}) | |
107 | u'3: $title' | |
108 | </pre></div> | |
109 | ||
110 | <P> | |
111 | <div class="seealso"> | |
112 | <p class="heading">See Also:</p> | |
113 | ||
114 | <dl compact="compact" class="seerfc"> | |
115 | <dt><a href="http://www.python.org/peps/pep-0292.html" | |
116 | title="Simpler String Substitutions" | |
117 | >PEP 292, <em>Simpler String Substitutions</em></a> | |
118 | <dd>Written and implemented | |
119 | by Barry Warsaw. | |
120 | </dl> | |
121 | </div> | |
122 | ||
123 | <P> | |
124 | ||
125 | <DIV CLASS="navigation"> | |
126 | <div class='online-navigation'> | |
127 | <p></p><hr /> | |
128 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
129 | <tr> | |
130 | <td class='online-navigation'><a rel="prev" title="3 PEP 289: Generator" | |
131 | href="node4.html"><img src='../icons/previous.png' | |
132 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
133 | <td class='online-navigation'><a rel="parent" title="What's New in Python" | |
134 | href="whatsnew24.html"><img src='../icons/up.png' | |
135 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
136 | <td class='online-navigation'><a rel="next" title="5 PEP 318: Decorators" | |
137 | href="node6.html"><img src='../icons/next.png' | |
138 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
139 | <td align="center" width="100%">What's New in Python 2.4</td> | |
140 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
141 | href="contents.html"><img src='../icons/contents.png' | |
142 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
143 | <td class='online-navigation'><img src='../icons/blank.png' | |
144 | border='0' height='32' alt='' width='32' /></td> | |
145 | <td class='online-navigation'><img src='../icons/blank.png' | |
146 | border='0' height='32' alt='' width='32' /></td> | |
147 | </tr></table> | |
148 | <div class='online-navigation'> | |
149 | <b class="navlabel">Previous:</b> | |
150 | <a class="sectref" rel="prev" href="node4.html">3 PEP 289: Generator</A> | |
151 | <b class="navlabel">Up:</b> | |
152 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> | |
153 | <b class="navlabel">Next:</b> | |
154 | <a class="sectref" rel="next" href="node6.html">5 PEP 318: Decorators</A> | |
155 | </div> | |
156 | </div> | |
157 | <hr /> | |
158 | <span class="release-info">Release 1.01.</span> | |
159 | </DIV> | |
160 | <!--End of Navigation Panel--> | |
161 | <ADDRESS> | |
162 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
163 | </ADDRESS> | |
164 | </BODY> | |
165 | </HTML> |