Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / typesseq-strings.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.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="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="typesseq-xrange.html" />
13<link rel="prev" href="string-methods.html" />
14<link rel="parent" href="typesseq.html" />
15<link rel="next" href="typesseq-xrange.html" />
16<meta name='aesop' content='information' />
17<title>2.3.6.2 String Formatting Operations </title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="2.3.6.1 String Methods"
25 href="string-methods.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="2.3.6 Sequence Types "
28 href="typesseq.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="2.3.6.3 XRange Type"
31 href="typesseq-xrange.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="string-methods.html">2.3.6.1 String Methods</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="typesseq.html">2.3.6 Sequence Types </A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="typesseq-xrange.html">2.3.6.3 XRange Type</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H3><A NAME="SECTION004362000000000000000"></A><A NAME="typesseq-strings"></A>
56<BR>
572.3.6.2 String Formatting Operations
58</H3>
59
60<P>
61<a id='l2h-211' xml:id='l2h-211'></a>
62
63<P>
64String and Unicode objects have one unique built-in operation: the
65<code>%</code> operator (modulo). This is also known as the string
66<em>formatting</em> or <em>interpolation</em> operator. Given
67<code><var>format</var> % <var>values</var></code> (where <var>format</var> is a string or
68Unicode object), <code>%</code> conversion specifications in <var>format</var>
69are replaced with zero or more elements of <var>values</var>. The effect
70is similar to the using <tt class="cfunction">sprintf()</tt> in the C language. If
71<var>format</var> is a Unicode object, or if any of the objects being
72converted using the <code>%s</code> conversion are Unicode objects, the
73result will also be a Unicode object.
74
75<P>
76If <var>format</var> requires a single argument, <var>values</var> may be a
77single non-tuple object.<A NAME="tex2html9"
78 HREF="#foot2430"><SUP>2.8</SUP></A> Otherwise, <var>values</var> must be a tuple with
79exactly the number of items specified by the format string, or a
80single mapping object (for example, a dictionary).
81
82<P>
83A conversion specifier contains two or more characters and has the
84following components, which must occur in this order:
85
86<P>
87
88<OL>
89<LI>The "<tt class="character">%</tt>" character, which marks the start of the
90 specifier.
91</LI>
92<LI>Mapping key (optional), consisting of a parenthesised sequence
93 of characters (for example, <code>(somename)</code>).
94</LI>
95<LI>Conversion flags (optional), which affect the result of some
96 conversion types.
97</LI>
98<LI>Minimum field width (optional). If specified as an
99 "<tt class="character">*</tt>" (asterisk), the actual width is read from the
100 next element of the tuple in <var>values</var>, and the object to
101 convert comes after the minimum field width and optional
102 precision.
103</LI>
104<LI>Precision (optional), given as a "<tt class="character">.</tt>" (dot) followed
105 by the precision. If specified as "<tt class="character">*</tt>" (an
106 asterisk), the actual width is read from the next element of
107 the tuple in <var>values</var>, and the value to convert comes after
108 the precision.
109</LI>
110<LI>Length modifier (optional).
111</LI>
112<LI>Conversion type.
113</LI>
114</OL>
115
116<P>
117When the right argument is a dictionary (or other mapping type), then
118the formats in the string <em>must</em> include a parenthesised mapping key into
119that dictionary inserted immediately after the "<tt class="character">%</tt>"
120character. The mapping key selects the value to be formatted from the
121mapping. For example:
122
123<P>
124<div class="verbatim"><pre>
125&gt;&gt;&gt; print '%(language)s has %(#)03d quote types.' % \
126 {'language': "Python", "#": 2}
127Python has 002 quote types.
128</pre></div>
129
130<P>
131In this case no <code>*</code> specifiers may occur in a format (since they
132require a sequential parameter list).
133
134<P>
135The conversion flag characters are:
136
137<P>
138<div class="center"><table class="realtable">
139 <thead>
140 <tr>
141 <th class="center">Flag</th>
142 <th class="left" >Meaning</th>
143 </tr>
144 </thead>
145 <tbody>
146 <tr><td class="center" valign="baseline"><tt class="character">#</tt></td>
147 <td class="left" >The value conversion will use the ``alternate form''
148 (where defined below).</td></tr>
149 <tr><td class="center" valign="baseline"><tt class="character">0</tt></td>
150 <td class="left" >The conversion will be zero padded for numeric values.</td></tr>
151 <tr><td class="center" valign="baseline"><tt class="character">-</tt></td>
152 <td class="left" >The converted value is left adjusted (overrides
153 the "<tt class="character">0</tt>" conversion if both are given).</td></tr>
154 <tr><td class="center" valign="baseline"><tt class="character">&nbsp;</tt></td>
155 <td class="left" >(a space) A blank should be left before a positive number
156 (or empty string) produced by a signed conversion.</td></tr>
157 <tr><td class="center" valign="baseline"><tt class="character">+</tt></td>
158 <td class="left" >A sign character ("<tt class="character">+</tt>" or "<tt class="character">-</tt>") will
159 precede the conversion (overrides a "space" flag).</td></tr></tbody>
160</table></div>
161
162<P>
163The length modifier may be <code>h</code>, <code>l</code>, and <code>L</code> may be
164present, but are ignored as they are not necessary for Python.
165
166<P>
167The conversion types are:
168
169<P>
170<div class="center"><table class="realtable">
171 <thead>
172 <tr>
173 <th class="center">Conversion</th>
174 <th class="left" >Meaning</th>
175 <th class="center">Notes</th>
176 </tr>
177 </thead>
178 <tbody>
179 <tr><td class="center" valign="baseline"><tt class="character">d</tt></td>
180 <td class="left" >Signed integer decimal.</td>
181 <td class="center"></td></tr>
182 <tr><td class="center" valign="baseline"><tt class="character">i</tt></td>
183 <td class="left" >Signed integer decimal.</td>
184 <td class="center"></td></tr>
185 <tr><td class="center" valign="baseline"><tt class="character">o</tt></td>
186 <td class="left" >Unsigned octal.</td>
187 <td class="center">(1)</td></tr>
188 <tr><td class="center" valign="baseline"><tt class="character">u</tt></td>
189 <td class="left" >Unsigned decimal.</td>
190 <td class="center"></td></tr>
191 <tr><td class="center" valign="baseline"><tt class="character">x</tt></td>
192 <td class="left" >Unsigned hexadecimal (lowercase).</td>
193 <td class="center">(2)</td></tr>
194 <tr><td class="center" valign="baseline"><tt class="character">X</tt></td>
195 <td class="left" >Unsigned hexadecimal (uppercase).</td>
196 <td class="center">(2)</td></tr>
197 <tr><td class="center" valign="baseline"><tt class="character">e</tt></td>
198 <td class="left" >Floating point exponential format (lowercase).</td>
199 <td class="center"></td></tr>
200 <tr><td class="center" valign="baseline"><tt class="character">E</tt></td>
201 <td class="left" >Floating point exponential format (uppercase).</td>
202 <td class="center"></td></tr>
203 <tr><td class="center" valign="baseline"><tt class="character">f</tt></td>
204 <td class="left" >Floating point decimal format.</td>
205 <td class="center"></td></tr>
206 <tr><td class="center" valign="baseline"><tt class="character">F</tt></td>
207 <td class="left" >Floating point decimal format.</td>
208 <td class="center"></td></tr>
209 <tr><td class="center" valign="baseline"><tt class="character">g</tt></td>
210 <td class="left" >Same as "<tt class="character">e</tt>" if exponent is greater than -4 or
211 less than precision, "<tt class="character">f</tt>" otherwise.</td>
212 <td class="center"></td></tr>
213 <tr><td class="center" valign="baseline"><tt class="character">G</tt></td>
214 <td class="left" >Same as "<tt class="character">E</tt>" if exponent is greater than -4 or
215 less than precision, "<tt class="character">F</tt>" otherwise.</td>
216 <td class="center"></td></tr>
217 <tr><td class="center" valign="baseline"><tt class="character">c</tt></td>
218 <td class="left" >Single character (accepts integer or single character
219 string).</td>
220 <td class="center"></td></tr>
221 <tr><td class="center" valign="baseline"><tt class="character">r</tt></td>
222 <td class="left" >String (converts any python object using
223 <tt class="function">repr()</tt>).</td>
224 <td class="center">(3)</td></tr>
225 <tr><td class="center" valign="baseline"><tt class="character">s</tt></td>
226 <td class="left" >String (converts any python object using
227 <tt class="function">str()</tt>).</td>
228 <td class="center">(4)</td></tr>
229 <tr><td class="center" valign="baseline"><tt class="character">%</tt></td>
230 <td class="left" >No argument is converted, results in a "<tt class="character">%</tt>"
231 character in the result.</td>
232 <td class="center"></td></tr></tbody>
233</table></div>
234
235<P>
236Notes:
237<DL>
238<DT><STRONG>(1)</STRONG></DT>
239<DD>The alternate form causes a leading zero ("<tt class="character">0</tt>") to be
240 inserted between left-hand padding and the formatting of the
241 number if the leading character of the result is not already a
242 zero.
243
244</DD>
245<DT><STRONG>(2)</STRONG></DT>
246<DD>The alternate form causes a leading <code>'0x'</code> or <code>'0X'</code>
247 (depending on whether the "<tt class="character">x</tt>" or "<tt class="character">X</tt>" format
248 was used) to be inserted between left-hand padding and the
249 formatting of the number if the leading character of the result is
250 not already a zero.
251
252</DD>
253<DT><STRONG>(3)</STRONG></DT>
254<DD>The <code>%r</code> conversion was added in Python 2.0.
255
256</DD>
257<DT><STRONG>(4)</STRONG></DT>
258<DD>If the object or format provided is a <tt class="class">unicode</tt> string,
259 the resulting string will also be <tt class="class">unicode</tt>.
260</DD>
261</DL>
262
263<P>
264Since Python strings have an explicit length, <code>%s</code> conversions
265do not assume that <code>'&#92;0'</code> is the end of the string.
266
267<P>
268For safety reasons, floating point precisions are clipped to 50;
269<code>%f</code> conversions for numbers whose absolute value is over 1e25
270are replaced by <code>%g</code> conversions.<A NAME="tex2html10"
271 HREF="#foot2537"><SUP>2.9</SUP></A> All other errors raise exceptions.
272
273<P>
274Additional string operations are defined in standard modules
275<tt class="module"><a href="module-string.html">string</a></tt><a id='l2h-212' xml:id='l2h-212'></a> and
276<tt class="module"><a href="module-re.html">re</a></tt>.<a id='l2h-213' xml:id='l2h-213'></a>
277
278<P>
279<BR><HR><H4>Footnotes</H4>
280<DL>
281<DT><A NAME="foot2430">... object.</A><A
282 href="typesseq-strings.html#tex2html9"><SUP>2.8</SUP></A></DT>
283<DD>To format only a tuple you
284should therefore provide a singleton tuple whose only element
285is the tuple to be formatted.
286
287</DD>
288<DT><A NAME="foot2537">... conversions.</A><A
289 href="typesseq-strings.html#tex2html10"><SUP>2.9</SUP></A></DT>
290<DD>
291 These numbers are fairly arbitrary. They are intended to
292 avoid printing endless strings of meaningless digits without hampering
293 correct use and without having to know the exact precision of floating
294 point values on a particular machine.
295
296
297</DD>
298</DL>
299<DIV CLASS="navigation">
300<div class='online-navigation'>
301<p></p><hr />
302<table align="center" width="100%" cellpadding="0" cellspacing="2">
303<tr>
304<td class='online-navigation'><a rel="prev" title="2.3.6.1 String Methods"
305 href="string-methods.html"><img src='../icons/previous.png'
306 border='0' height='32' alt='Previous Page' width='32' /></A></td>
307<td class='online-navigation'><a rel="parent" title="2.3.6 Sequence Types "
308 href="typesseq.html"><img src='../icons/up.png'
309 border='0' height='32' alt='Up One Level' width='32' /></A></td>
310<td class='online-navigation'><a rel="next" title="2.3.6.3 XRange Type"
311 href="typesseq-xrange.html"><img src='../icons/next.png'
312 border='0' height='32' alt='Next Page' width='32' /></A></td>
313<td align="center" width="100%">Python Library Reference</td>
314<td class='online-navigation'><a rel="contents" title="Table of Contents"
315 href="contents.html"><img src='../icons/contents.png'
316 border='0' height='32' alt='Contents' width='32' /></A></td>
317<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
318 border='0' height='32' alt='Module Index' width='32' /></a></td>
319<td class='online-navigation'><a rel="index" title="Index"
320 href="genindex.html"><img src='../icons/index.png'
321 border='0' height='32' alt='Index' width='32' /></A></td>
322</tr></table>
323<div class='online-navigation'>
324<b class="navlabel">Previous:</b>
325<a class="sectref" rel="prev" href="string-methods.html">2.3.6.1 String Methods</A>
326<b class="navlabel">Up:</b>
327<a class="sectref" rel="parent" href="typesseq.html">2.3.6 Sequence Types </A>
328<b class="navlabel">Next:</b>
329<a class="sectref" rel="next" href="typesseq-xrange.html">2.3.6.3 XRange Type</A>
330</div>
331</div>
332<hr />
333<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
334</DIV>
335<!--End of Navigation Panel-->
336<ADDRESS>
337See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
338</ADDRESS>
339</BODY>
340</HTML>