PageRenderTime 34ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/python/library/string.html

https://bitbucket.org/thncr/manuals
HTML | 638 lines | 608 code | 30 blank | 0 comment | 0 complexity | d9caf2b981452a85cd0b811b46104a34 MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>7.1. string Common string operations &mdash; Python v2.7.3 documentation</title>
  7. <link rel="stylesheet" href="../_static/default.css" type="text/css" />
  8. <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  9. <script type="text/javascript">
  10. var DOCUMENTATION_OPTIONS = {
  11. URL_ROOT: '../',
  12. VERSION: '2.7.3',
  13. COLLAPSE_INDEX: false,
  14. FILE_SUFFIX: '.html',
  15. HAS_SOURCE: true
  16. };
  17. </script>
  18. <script type="text/javascript" src="../_static/jquery.js"></script>
  19. <script type="text/javascript" src="../_static/underscore.js"></script>
  20. <script type="text/javascript" src="../_static/doctools.js"></script>
  21. <script type="text/javascript" src="../_static/sidebar.js"></script>
  22. <link rel="search" type="application/opensearchdescription+xml"
  23. title="Search within Python v2.7.3 documentation"
  24. href="../_static/opensearch.xml"/>
  25. <link rel="author" title="About these documents" href="../about.html" />
  26. <link rel="copyright" title="Copyright" href="../copyright.html" />
  27. <link rel="top" title="Python v2.7.3 documentation" href="../index.html" />
  28. <link rel="up" title="7. String Services" href="strings.html" />
  29. <link rel="next" title="7.2. re — Regular expression operations" href="re.html" />
  30. <link rel="prev" title="7. String Services" href="strings.html" />
  31. <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
  32. <script type="text/javascript" src="../_static/copybutton.js"></script>
  33. <script type="text/javascript" src="../_static/version_switch.js"></script>
  34. </head>
  35. <body>
  36. <div class="related">
  37. <h3>Navigation</h3>
  38. <ul>
  39. <li class="right" style="margin-right: 10px">
  40. <a href="../genindex.html" title="General Index"
  41. accesskey="I">index</a></li>
  42. <li class="right" >
  43. <a href="../py-modindex.html" title="Python Module Index"
  44. >modules</a> |</li>
  45. <li class="right" >
  46. <a href="re.html" title="7.2. re — Regular expression operations"
  47. accesskey="N">next</a> |</li>
  48. <li class="right" >
  49. <a href="strings.html" title="7. String Services"
  50. accesskey="P">previous</a> |</li>
  51. <li><img src="../_static/py.png" alt=""
  52. style="vertical-align: middle; margin-top: -1px"/></li>
  53. <li><a href="http://www.python.org/">Python</a> &raquo;</li>
  54. <li>
  55. <span class="version_switcher_placeholder">2.7.3</span>
  56. <a href="../index.html">Documentation</a> &raquo;
  57. </li>
  58. <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
  59. <li><a href="strings.html" accesskey="U">7. String Services</a> &raquo;</li>
  60. </ul>
  61. </div>
  62. <div class="document">
  63. <div class="documentwrapper">
  64. <div class="bodywrapper">
  65. <div class="body">
  66. <div class="section" id="module-string">
  67. <span id="string-common-string-operations"></span><h1>7.1. <a class="reference internal" href="#module-string" title="string: Common string operations."><tt class="xref py py-mod docutils literal"><span class="pre">string</span></tt></a> &#8212; Common string operations<a class="headerlink" href="#module-string" title="Permalink to this headline"></a></h1>
  68. <p id="index-0"><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/2.7/Lib/string.py">Lib/string.py</a></p>
  69. <hr class="docutils" />
  70. <p>The <a class="reference internal" href="#module-string" title="string: Common string operations."><tt class="xref py py-mod docutils literal"><span class="pre">string</span></tt></a> module contains a number of useful constants and
  71. classes, as well as some deprecated legacy functions that are also
  72. available as methods on strings. In addition, Python&#8217;s built-in string
  73. classes support the sequence type methods described in the
  74. <a class="reference internal" href="stdtypes.html#typesseq"><em>Sequence Types &#8212; str, unicode, list, tuple, bytearray, buffer, xrange</em></a> section, and also the string-specific methods described
  75. in the <a class="reference internal" href="stdtypes.html#string-methods"><em>String Methods</em></a> section. To output formatted strings use
  76. template strings or the <tt class="docutils literal"><span class="pre">%</span></tt> operator described in the
  77. <a class="reference internal" href="stdtypes.html#string-formatting"><em>String Formatting Operations</em></a> section. Also, see the <a class="reference internal" href="re.html#module-re" title="re: Regular expression operations."><tt class="xref py py-mod docutils literal"><span class="pre">re</span></tt></a> module for
  78. string functions based on regular expressions.</p>
  79. <div class="section" id="string-constants">
  80. <h2>7.1.1. String constants<a class="headerlink" href="#string-constants" title="Permalink to this headline"></a></h2>
  81. <p>The constants defined in this module are:</p>
  82. <dl class="data">
  83. <dt id="string.ascii_letters">
  84. <tt class="descclassname">string.</tt><tt class="descname">ascii_letters</tt><a class="headerlink" href="#string.ascii_letters" title="Permalink to this definition"></a></dt>
  85. <dd><p>The concatenation of the <a class="reference internal" href="#string.ascii_lowercase" title="string.ascii_lowercase"><tt class="xref py py-const docutils literal"><span class="pre">ascii_lowercase</span></tt></a> and <a class="reference internal" href="#string.ascii_uppercase" title="string.ascii_uppercase"><tt class="xref py py-const docutils literal"><span class="pre">ascii_uppercase</span></tt></a>
  86. constants described below. This value is not locale-dependent.</p>
  87. </dd></dl>
  88. <dl class="data">
  89. <dt id="string.ascii_lowercase">
  90. <tt class="descclassname">string.</tt><tt class="descname">ascii_lowercase</tt><a class="headerlink" href="#string.ascii_lowercase" title="Permalink to this definition"></a></dt>
  91. <dd><p>The lowercase letters <tt class="docutils literal"><span class="pre">'abcdefghijklmnopqrstuvwxyz'</span></tt>. This value is not
  92. locale-dependent and will not change.</p>
  93. </dd></dl>
  94. <dl class="data">
  95. <dt id="string.ascii_uppercase">
  96. <tt class="descclassname">string.</tt><tt class="descname">ascii_uppercase</tt><a class="headerlink" href="#string.ascii_uppercase" title="Permalink to this definition"></a></dt>
  97. <dd><p>The uppercase letters <tt class="docutils literal"><span class="pre">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span></tt>. This value is not
  98. locale-dependent and will not change.</p>
  99. </dd></dl>
  100. <dl class="data">
  101. <dt id="string.digits">
  102. <tt class="descclassname">string.</tt><tt class="descname">digits</tt><a class="headerlink" href="#string.digits" title="Permalink to this definition"></a></dt>
  103. <dd><p>The string <tt class="docutils literal"><span class="pre">'0123456789'</span></tt>.</p>
  104. </dd></dl>
  105. <dl class="data">
  106. <dt id="string.hexdigits">
  107. <tt class="descclassname">string.</tt><tt class="descname">hexdigits</tt><a class="headerlink" href="#string.hexdigits" title="Permalink to this definition"></a></dt>
  108. <dd><p>The string <tt class="docutils literal"><span class="pre">'0123456789abcdefABCDEF'</span></tt>.</p>
  109. </dd></dl>
  110. <dl class="data">
  111. <dt id="string.letters">
  112. <tt class="descclassname">string.</tt><tt class="descname">letters</tt><a class="headerlink" href="#string.letters" title="Permalink to this definition"></a></dt>
  113. <dd><p>The concatenation of the strings <a class="reference internal" href="#string.lowercase" title="string.lowercase"><tt class="xref py py-const docutils literal"><span class="pre">lowercase</span></tt></a> and <a class="reference internal" href="#string.uppercase" title="string.uppercase"><tt class="xref py py-const docutils literal"><span class="pre">uppercase</span></tt></a>
  114. described below. The specific value is locale-dependent, and will be updated
  115. when <a class="reference internal" href="locale.html#locale.setlocale" title="locale.setlocale"><tt class="xref py py-func docutils literal"><span class="pre">locale.setlocale()</span></tt></a> is called.</p>
  116. </dd></dl>
  117. <dl class="data">
  118. <dt id="string.lowercase">
  119. <tt class="descclassname">string.</tt><tt class="descname">lowercase</tt><a class="headerlink" href="#string.lowercase" title="Permalink to this definition"></a></dt>
  120. <dd><p>A string containing all the characters that are considered lowercase letters.
  121. On most systems this is the string <tt class="docutils literal"><span class="pre">'abcdefghijklmnopqrstuvwxyz'</span></tt>. The
  122. specific value is locale-dependent, and will be updated when
  123. <a class="reference internal" href="locale.html#locale.setlocale" title="locale.setlocale"><tt class="xref py py-func docutils literal"><span class="pre">locale.setlocale()</span></tt></a> is called.</p>
  124. </dd></dl>
  125. <dl class="data">
  126. <dt id="string.octdigits">
  127. <tt class="descclassname">string.</tt><tt class="descname">octdigits</tt><a class="headerlink" href="#string.octdigits" title="Permalink to this definition"></a></dt>
  128. <dd><p>The string <tt class="docutils literal"><span class="pre">'01234567'</span></tt>.</p>
  129. </dd></dl>
  130. <dl class="data">
  131. <dt id="string.punctuation">
  132. <tt class="descclassname">string.</tt><tt class="descname">punctuation</tt><a class="headerlink" href="#string.punctuation" title="Permalink to this definition"></a></dt>
  133. <dd><p>String of ASCII characters which are considered punctuation characters in the
  134. <tt class="docutils literal"><span class="pre">C</span></tt> locale.</p>
  135. </dd></dl>
  136. <dl class="data">
  137. <dt id="string.printable">
  138. <tt class="descclassname">string.</tt><tt class="descname">printable</tt><a class="headerlink" href="#string.printable" title="Permalink to this definition"></a></dt>
  139. <dd><p>String of characters which are considered printable. This is a combination of
  140. <a class="reference internal" href="#string.digits" title="string.digits"><tt class="xref py py-const docutils literal"><span class="pre">digits</span></tt></a>, <a class="reference internal" href="#string.letters" title="string.letters"><tt class="xref py py-const docutils literal"><span class="pre">letters</span></tt></a>, <a class="reference internal" href="#string.punctuation" title="string.punctuation"><tt class="xref py py-const docutils literal"><span class="pre">punctuation</span></tt></a>, and
  141. <a class="reference internal" href="#string.whitespace" title="string.whitespace"><tt class="xref py py-const docutils literal"><span class="pre">whitespace</span></tt></a>.</p>
  142. </dd></dl>
  143. <dl class="data">
  144. <dt id="string.uppercase">
  145. <tt class="descclassname">string.</tt><tt class="descname">uppercase</tt><a class="headerlink" href="#string.uppercase" title="Permalink to this definition"></a></dt>
  146. <dd><p>A string containing all the characters that are considered uppercase letters.
  147. On most systems this is the string <tt class="docutils literal"><span class="pre">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span></tt>. The
  148. specific value is locale-dependent, and will be updated when
  149. <a class="reference internal" href="locale.html#locale.setlocale" title="locale.setlocale"><tt class="xref py py-func docutils literal"><span class="pre">locale.setlocale()</span></tt></a> is called.</p>
  150. </dd></dl>
  151. <dl class="data">
  152. <dt id="string.whitespace">
  153. <tt class="descclassname">string.</tt><tt class="descname">whitespace</tt><a class="headerlink" href="#string.whitespace" title="Permalink to this definition"></a></dt>
  154. <dd><p>A string containing all characters that are considered whitespace. On most
  155. systems this includes the characters space, tab, linefeed, return, formfeed, and
  156. vertical tab.</p>
  157. </dd></dl>
  158. </div>
  159. <div class="section" id="string-formatting">
  160. <span id="new-string-formatting"></span><h2>7.1.2. String Formatting<a class="headerlink" href="#string-formatting" title="Permalink to this headline"></a></h2>
  161. <p class="versionadded">
  162. <span class="versionmodified">New in version 2.6.</span></p>
  163. <p>The built-in str and unicode classes provide the ability
  164. to do complex variable substitutions and value formatting via the
  165. <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a> method described in <span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a>. The <a class="reference internal" href="#string.Formatter" title="string.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a>
  166. class in the <a class="reference internal" href="#module-string" title="string: Common string operations."><tt class="xref py py-mod docutils literal"><span class="pre">string</span></tt></a> module allows you to create and customize your own
  167. string formatting behaviors using the same implementation as the built-in
  168. <a class="reference internal" href="functions.html#format" title="format"><tt class="xref py py-meth docutils literal"><span class="pre">format()</span></tt></a> method.</p>
  169. <dl class="class">
  170. <dt id="string.Formatter">
  171. <em class="property">class </em><tt class="descclassname">string.</tt><tt class="descname">Formatter</tt><a class="headerlink" href="#string.Formatter" title="Permalink to this definition"></a></dt>
  172. <dd><p>The <a class="reference internal" href="#string.Formatter" title="string.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> class has the following public methods:</p>
  173. <dl class="method">
  174. <dt id="string.Formatter.format">
  175. <tt class="descname">format</tt><big>(</big><em>format_string</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.format" title="Permalink to this definition"></a></dt>
  176. <dd><p><a class="reference internal" href="functions.html#format" title="format"><tt class="xref py py-meth docutils literal"><span class="pre">format()</span></tt></a> is the primary API method. It takes a format string and
  177. an arbitrary set of positional and keyword arguments.
  178. <a class="reference internal" href="functions.html#format" title="format"><tt class="xref py py-meth docutils literal"><span class="pre">format()</span></tt></a> is just a wrapper that calls <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><tt class="xref py py-meth docutils literal"><span class="pre">vformat()</span></tt></a>.</p>
  179. </dd></dl>
  180. <dl class="method">
  181. <dt id="string.Formatter.vformat">
  182. <tt class="descname">vformat</tt><big>(</big><em>format_string</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.vformat" title="Permalink to this definition"></a></dt>
  183. <dd><p>This function does the actual work of formatting. It is exposed as a
  184. separate function for cases where you want to pass in a predefined
  185. dictionary of arguments, rather than unpacking and repacking the
  186. dictionary as individual arguments using the <tt class="docutils literal"><span class="pre">*args</span></tt> and <tt class="docutils literal"><span class="pre">**kwargs</span></tt>
  187. syntax. <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><tt class="xref py py-meth docutils literal"><span class="pre">vformat()</span></tt></a> does the work of breaking up the format string
  188. into character data and replacement fields. It calls the various
  189. methods described below.</p>
  190. </dd></dl>
  191. <p>In addition, the <a class="reference internal" href="#string.Formatter" title="string.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> defines a number of methods that are
  192. intended to be replaced by subclasses:</p>
  193. <dl class="method">
  194. <dt id="string.Formatter.parse">
  195. <tt class="descname">parse</tt><big>(</big><em>format_string</em><big>)</big><a class="headerlink" href="#string.Formatter.parse" title="Permalink to this definition"></a></dt>
  196. <dd><p>Loop over the format_string and return an iterable of tuples
  197. (<em>literal_text</em>, <em>field_name</em>, <em>format_spec</em>, <em>conversion</em>). This is used
  198. by <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><tt class="xref py py-meth docutils literal"><span class="pre">vformat()</span></tt></a> to break the string into either literal text, or
  199. replacement fields.</p>
  200. <p>The values in the tuple conceptually represent a span of literal text
  201. followed by a single replacement field. If there is no literal text
  202. (which can happen if two replacement fields occur consecutively), then
  203. <em>literal_text</em> will be a zero-length string. If there is no replacement
  204. field, then the values of <em>field_name</em>, <em>format_spec</em> and <em>conversion</em>
  205. will be <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
  206. </dd></dl>
  207. <dl class="method">
  208. <dt id="string.Formatter.get_field">
  209. <tt class="descname">get_field</tt><big>(</big><em>field_name</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.get_field" title="Permalink to this definition"></a></dt>
  210. <dd><p>Given <em>field_name</em> as returned by <a class="reference internal" href="#string.Formatter.parse" title="string.Formatter.parse"><tt class="xref py py-meth docutils literal"><span class="pre">parse()</span></tt></a> (see above), convert it to
  211. an object to be formatted. Returns a tuple (obj, used_key). The default
  212. version takes strings of the form defined in <span class="target" id="index-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a>, such as
  213. &#8220;0[name]&#8221; or &#8220;label.title&#8221;. <em>args</em> and <em>kwargs</em> are as passed in to
  214. <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><tt class="xref py py-meth docutils literal"><span class="pre">vformat()</span></tt></a>. The return value <em>used_key</em> has the same meaning as the
  215. <em>key</em> parameter to <a class="reference internal" href="#string.Formatter.get_value" title="string.Formatter.get_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_value()</span></tt></a>.</p>
  216. </dd></dl>
  217. <dl class="method">
  218. <dt id="string.Formatter.get_value">
  219. <tt class="descname">get_value</tt><big>(</big><em>key</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.get_value" title="Permalink to this definition"></a></dt>
  220. <dd><p>Retrieve a given field value. The <em>key</em> argument will be either an
  221. integer or a string. If it is an integer, it represents the index of the
  222. positional argument in <em>args</em>; if it is a string, then it represents a
  223. named argument in <em>kwargs</em>.</p>
  224. <p>The <em>args</em> parameter is set to the list of positional arguments to
  225. <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><tt class="xref py py-meth docutils literal"><span class="pre">vformat()</span></tt></a>, and the <em>kwargs</em> parameter is set to the dictionary of
  226. keyword arguments.</p>
  227. <p>For compound field names, these functions are only called for the first
  228. component of the field name; Subsequent components are handled through
  229. normal attribute and indexing operations.</p>
  230. <p>So for example, the field expression &#8216;0.name&#8217; would cause
  231. <a class="reference internal" href="#string.Formatter.get_value" title="string.Formatter.get_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_value()</span></tt></a> to be called with a <em>key</em> argument of 0. The <tt class="docutils literal"><span class="pre">name</span></tt>
  232. attribute will be looked up after <a class="reference internal" href="#string.Formatter.get_value" title="string.Formatter.get_value"><tt class="xref py py-meth docutils literal"><span class="pre">get_value()</span></tt></a> returns by calling the
  233. built-in <a class="reference internal" href="functions.html#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a> function.</p>
  234. <p>If the index or keyword refers to an item that does not exist, then an
  235. <a class="reference internal" href="exceptions.html#exceptions.IndexError" title="exceptions.IndexError"><tt class="xref py py-exc docutils literal"><span class="pre">IndexError</span></tt></a> or <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> should be raised.</p>
  236. </dd></dl>
  237. <dl class="method">
  238. <dt id="string.Formatter.check_unused_args">
  239. <tt class="descname">check_unused_args</tt><big>(</big><em>used_args</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.check_unused_args" title="Permalink to this definition"></a></dt>
  240. <dd><p>Implement checking for unused arguments if desired. The arguments to this
  241. function is the set of all argument keys that were actually referred to in
  242. the format string (integers for positional arguments, and strings for
  243. named arguments), and a reference to the <em>args</em> and <em>kwargs</em> that was
  244. passed to vformat. The set of unused args can be calculated from these
  245. parameters. <a class="reference internal" href="#string.Formatter.check_unused_args" title="string.Formatter.check_unused_args"><tt class="xref py py-meth docutils literal"><span class="pre">check_unused_args()</span></tt></a> is assumed to raise an exception if
  246. the check fails.</p>
  247. </dd></dl>
  248. <dl class="method">
  249. <dt id="string.Formatter.format_field">
  250. <tt class="descname">format_field</tt><big>(</big><em>value</em>, <em>format_spec</em><big>)</big><a class="headerlink" href="#string.Formatter.format_field" title="Permalink to this definition"></a></dt>
  251. <dd><p><a class="reference internal" href="#string.Formatter.format_field" title="string.Formatter.format_field"><tt class="xref py py-meth docutils literal"><span class="pre">format_field()</span></tt></a> simply calls the global <a class="reference internal" href="functions.html#format" title="format"><tt class="xref py py-func docutils literal"><span class="pre">format()</span></tt></a> built-in. The
  252. method is provided so that subclasses can override it.</p>
  253. </dd></dl>
  254. <dl class="method">
  255. <dt id="string.Formatter.convert_field">
  256. <tt class="descname">convert_field</tt><big>(</big><em>value</em>, <em>conversion</em><big>)</big><a class="headerlink" href="#string.Formatter.convert_field" title="Permalink to this definition"></a></dt>
  257. <dd><p>Converts the value (returned by <a class="reference internal" href="#string.Formatter.get_field" title="string.Formatter.get_field"><tt class="xref py py-meth docutils literal"><span class="pre">get_field()</span></tt></a>) given a conversion type
  258. (as in the tuple returned by the <a class="reference internal" href="#string.Formatter.parse" title="string.Formatter.parse"><tt class="xref py py-meth docutils literal"><span class="pre">parse()</span></tt></a> method). The default
  259. version understands &#8216;s&#8217; (str), &#8216;r&#8217; (repr) and &#8216;a&#8217; (ascii) conversion
  260. types.</p>
  261. </dd></dl>
  262. </dd></dl>
  263. </div>
  264. <div class="section" id="format-string-syntax">
  265. <span id="formatstrings"></span><h2>7.1.3. Format String Syntax<a class="headerlink" href="#format-string-syntax" title="Permalink to this headline"></a></h2>
  266. <p>The <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a> method and the <a class="reference internal" href="#string.Formatter" title="string.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> class share the same
  267. syntax for format strings (although in the case of <a class="reference internal" href="#string.Formatter" title="string.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a>,
  268. subclasses can define their own format string syntax).</p>
  269. <p>Format strings contain &#8220;replacement fields&#8221; surrounded by curly braces <tt class="docutils literal"><span class="pre">{}</span></tt>.
  270. Anything that is not contained in braces is considered literal text, which is
  271. copied unchanged to the output. If you need to include a brace character in the
  272. literal text, it can be escaped by doubling: <tt class="docutils literal"><span class="pre">{{</span></tt> and <tt class="docutils literal"><span class="pre">}}</span></tt>.</p>
  273. <p>The grammar for a replacement field is as follows:</p>
  274. <blockquote>
  275. <div><pre>
  276. <strong id="grammar-token-replacement_field">replacement_field</strong> ::= &quot;{&quot; [<a class="reference internal" href="#grammar-token-field_name"><tt class="xref docutils literal"><span class="pre">field_name</span></tt></a>] [&quot;!&quot; <a class="reference internal" href="#grammar-token-conversion"><tt class="xref docutils literal"><span class="pre">conversion</span></tt></a>] [&quot;:&quot; <a class="reference internal" href="#grammar-token-format_spec"><tt class="xref docutils literal"><span class="pre">format_spec</span></tt></a>] &quot;}&quot;
  277. <strong id="grammar-token-field_name">field_name </strong> ::= arg_name (&quot;.&quot; <a class="reference internal" href="#grammar-token-attribute_name"><tt class="xref docutils literal"><span class="pre">attribute_name</span></tt></a> | &quot;[&quot; <a class="reference internal" href="#grammar-token-element_index"><tt class="xref docutils literal"><span class="pre">element_index</span></tt></a> &quot;]&quot;)*
  278. <strong id="grammar-token-arg_name">arg_name </strong> ::= [<a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a> | <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-integer"><tt class="xref docutils literal"><span class="pre">integer</span></tt></a>]
  279. <strong id="grammar-token-attribute_name">attribute_name </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a>
  280. <strong id="grammar-token-element_index">element_index </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-integer"><tt class="xref docutils literal"><span class="pre">integer</span></tt></a> | <a class="reference internal" href="#grammar-token-index_string"><tt class="xref docutils literal"><span class="pre">index_string</span></tt></a>
  281. <strong id="grammar-token-index_string">index_string </strong> ::= &lt;any source character except &quot;]&quot;&gt; +
  282. <strong id="grammar-token-conversion">conversion </strong> ::= &quot;r&quot; | &quot;s&quot;
  283. <strong id="grammar-token-format_spec">format_spec </strong> ::= &lt;described in the next section&gt;
  284. </pre>
  285. </div></blockquote>
  286. <p>In less formal terms, the replacement field can start with a <em>field_name</em> that specifies
  287. the object whose value is to be formatted and inserted
  288. into the output instead of the replacement field.
  289. The <em>field_name</em> is optionally followed by a <em>conversion</em> field, which is
  290. preceded by an exclamation point <tt class="docutils literal"><span class="pre">'!'</span></tt>, and a <em>format_spec</em>, which is preceded
  291. by a colon <tt class="docutils literal"><span class="pre">':'</span></tt>. These specify a non-default format for the replacement value.</p>
  292. <p>See also the <a class="reference internal" href="#formatspec"><em>Format Specification Mini-Language</em></a> section.</p>
  293. <p>The <em>field_name</em> itself begins with an <em>arg_name</em> that is either a number or a
  294. keyword. If it&#8217;s a number, it refers to a positional argument, and if it&#8217;s a keyword,
  295. it refers to a named keyword argument. If the numerical arg_names in a format string
  296. are 0, 1, 2, ... in sequence, they can all be omitted (not just some)
  297. and the numbers 0, 1, 2, ... will be automatically inserted in that order.
  298. Because <em>arg_name</em> is not quote-delimited, it is not possible to specify arbitrary
  299. dictionary keys (e.g., the strings <tt class="docutils literal"><span class="pre">'10'</span></tt> or <tt class="docutils literal"><span class="pre">':-]'</span></tt>) within a format string.
  300. The <em>arg_name</em> can be followed by any number of index or
  301. attribute expressions. An expression of the form <tt class="docutils literal"><span class="pre">'.name'</span></tt> selects the named
  302. attribute using <a class="reference internal" href="functions.html#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a>, while an expression of the form <tt class="docutils literal"><span class="pre">'[index]'</span></tt>
  303. does an index lookup using <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-func docutils literal"><span class="pre">__getitem__()</span></tt></a>.</p>
  304. <p class="versionchanged">
  305. <span class="versionmodified">Changed in version 2.7: </span>The positional argument specifiers can be omitted, so <tt class="docutils literal"><span class="pre">'{}</span> <span class="pre">{}'</span></tt> is
  306. equivalent to <tt class="docutils literal"><span class="pre">'{0}</span> <span class="pre">{1}'</span></tt>.</p>
  307. <p>Some simple format string examples:</p>
  308. <div class="highlight-python"><div class="highlight"><pre><span class="s">&quot;First, thou shalt count to {0}&quot;</span> <span class="c"># References first positional argument</span>
  309. <span class="s">&quot;Bring me a {}&quot;</span> <span class="c"># Implicitly references the first positional argument</span>
  310. <span class="s">&quot;From {} to {}&quot;</span> <span class="c"># Same as &quot;From {0} to {1}&quot;</span>
  311. <span class="s">&quot;My quest is {name}&quot;</span> <span class="c"># References keyword argument &#39;name&#39;</span>
  312. <span class="s">&quot;Weight in tons {0.weight}&quot;</span> <span class="c"># &#39;weight&#39; attribute of first positional arg</span>
  313. <span class="s">&quot;Units destroyed: {players[0]}&quot;</span> <span class="c"># First element of keyword argument &#39;players&#39;.</span>
  314. </pre></div>
  315. </div>
  316. <p>The <em>conversion</em> field causes a type coercion before formatting. Normally, the
  317. job of formatting a value is done by the <tt class="xref py py-meth docutils literal"><span class="pre">__format__()</span></tt> method of the value
  318. itself. However, in some cases it is desirable to force a type to be formatted
  319. as a string, overriding its own definition of formatting. By converting the
  320. value to a string before calling <tt class="xref py py-meth docutils literal"><span class="pre">__format__()</span></tt>, the normal formatting logic
  321. is bypassed.</p>
  322. <p>Two conversion flags are currently supported: <tt class="docutils literal"><span class="pre">'!s'</span></tt> which calls <a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a>
  323. on the value, and <tt class="docutils literal"><span class="pre">'!r'</span></tt> which calls <a class="reference internal" href="repr.html#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a>.</p>
  324. <p>Some examples:</p>
  325. <div class="highlight-python"><div class="highlight"><pre><span class="s">&quot;Harold&#39;s a clever {0!s}&quot;</span> <span class="c"># Calls str() on the argument first</span>
  326. <span class="s">&quot;Bring out the holy {name!r}&quot;</span> <span class="c"># Calls repr() on the argument first</span>
  327. </pre></div>
  328. </div>
  329. <p>The <em>format_spec</em> field contains a specification of how the value should be
  330. presented, including such details as field width, alignment, padding, decimal
  331. precision and so on. Each value type can define its own &#8220;formatting
  332. mini-language&#8221; or interpretation of the <em>format_spec</em>.</p>
  333. <p>Most built-in types support a common formatting mini-language, which is
  334. described in the next section.</p>
  335. <p>A <em>format_spec</em> field can also include nested replacement fields within it.
  336. These nested replacement fields can contain only a field name; conversion flags
  337. and format specifications are not allowed. The replacement fields within the
  338. format_spec are substituted before the <em>format_spec</em> string is interpreted.
  339. This allows the formatting of a value to be dynamically specified.</p>
  340. <p>See the <a class="reference internal" href="#formatexamples"><em>Format examples</em></a> section for some examples.</p>
  341. <div class="section" id="format-specification-mini-language">
  342. <span id="formatspec"></span><h3>7.1.3.1. Format Specification Mini-Language<a class="headerlink" href="#format-specification-mini-language" title="Permalink to this headline"></a></h3>
  343. <p>&#8220;Format specifications&#8221; are used within replacement fields contained within a
  344. format string to define how individual values are presented (see
  345. <a class="reference internal" href="#formatstrings"><em>Format String Syntax</em></a>). They can also be passed directly to the built-in
  346. <a class="reference internal" href="functions.html#format" title="format"><tt class="xref py py-func docutils literal"><span class="pre">format()</span></tt></a> function. Each formattable type may define how the format
  347. specification is to be interpreted.</p>
  348. <p>Most built-in types implement the following options for format specifications,
  349. although some of the formatting options are only supported by the numeric types.</p>
  350. <p>A general convention is that an empty format string (<tt class="docutils literal"><span class="pre">&quot;&quot;</span></tt>) produces
  351. the same result as if you had called <a class="reference internal" href="functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> on the value. A
  352. non-empty format string typically modifies the result.</p>
  353. <p>The general form of a <em>standard format specifier</em> is:</p>
  354. <pre>
  355. <strong id="id1">format_spec</strong> ::= [[<a class="reference internal" href="#grammar-token-fill"><tt class="xref docutils literal"><span class="pre">fill</span></tt></a>]<a class="reference internal" href="#grammar-token-align"><tt class="xref docutils literal"><span class="pre">align</span></tt></a>][<a class="reference internal" href="#grammar-token-sign"><tt class="xref docutils literal"><span class="pre">sign</span></tt></a>][#][0][<a class="reference internal" href="#grammar-token-width"><tt class="xref docutils literal"><span class="pre">width</span></tt></a>][,][.<a class="reference internal" href="#grammar-token-precision"><tt class="xref docutils literal"><span class="pre">precision</span></tt></a>][<a class="reference internal" href="#grammar-token-type"><tt class="xref docutils literal"><span class="pre">type</span></tt></a>]
  356. <strong id="grammar-token-fill">fill </strong> ::= &lt;a character other than '{' or '}'&gt;
  357. <strong id="grammar-token-align">align </strong> ::= &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;=&quot; | &quot;^&quot;
  358. <strong id="grammar-token-sign">sign </strong> ::= &quot;+&quot; | &quot;-&quot; | &quot; &quot;
  359. <strong id="grammar-token-width">width </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-integer"><tt class="xref docutils literal"><span class="pre">integer</span></tt></a>
  360. <strong id="grammar-token-precision">precision </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-integer"><tt class="xref docutils literal"><span class="pre">integer</span></tt></a>
  361. <strong id="grammar-token-type">type </strong> ::= &quot;b&quot; | &quot;c&quot; | &quot;d&quot; | &quot;e&quot; | &quot;E&quot; | &quot;f&quot; | &quot;F&quot; | &quot;g&quot; | &quot;G&quot; | &quot;n&quot; | &quot;o&quot; | &quot;s&quot; | &quot;x&quot; | &quot;X&quot; | &quot;%&quot;
  362. </pre>
  363. <p>The <em>fill</em> character can be any character other than &#8216;{&#8216; or &#8216;}&#8217;. The presence
  364. of a fill character is signaled by the character following it, which must be
  365. one of the alignment options. If the second character of <em>format_spec</em> is not
  366. a valid alignment option, then it is assumed that both the fill character and
  367. the alignment option are absent.</p>
  368. <p>The meaning of the various alignment options is as follows:</p>
  369. <blockquote>
  370. <div><table border="1" class="docutils">
  371. <colgroup>
  372. <col width="13%" />
  373. <col width="87%" />
  374. </colgroup>
  375. <thead valign="bottom">
  376. <tr><th class="head">Option</th>
  377. <th class="head">Meaning</th>
  378. </tr>
  379. </thead>
  380. <tbody valign="top">
  381. <tr><td><tt class="docutils literal"><span class="pre">'&lt;'</span></tt></td>
  382. <td>Forces the field to be left-aligned within the available
  383. space (this is the default for most objects).</td>
  384. </tr>
  385. <tr><td><tt class="docutils literal"><span class="pre">'&gt;'</span></tt></td>
  386. <td>Forces the field to be right-aligned within the
  387. available space (this is the default for numbers).</td>
  388. </tr>
  389. <tr><td><tt class="docutils literal"><span class="pre">'='</span></tt></td>
  390. <td>Forces the padding to be placed after the sign (if any)
  391. but before the digits. This is used for printing fields
  392. in the form &#8216;+000000120&#8217;. This alignment option is only
  393. valid for numeric types.</td>
  394. </tr>
  395. <tr><td><tt class="docutils literal"><span class="pre">'^'</span></tt></td>
  396. <td>Forces the field to be centered within the available
  397. space.</td>
  398. </tr>
  399. </tbody>
  400. </table>
  401. </div></blockquote>
  402. <p>Note that unless a minimum field width is defined, the field width will always
  403. be the same size as the data to fill it, so that the alignment option has no
  404. meaning in this case.</p>
  405. <p>The <em>sign</em> option is only valid for number types, and can be one of the
  406. following:</p>
  407. <blockquote>
  408. <div><table border="1" class="docutils">
  409. <colgroup>
  410. <col width="13%" />
  411. <col width="87%" />
  412. </colgroup>
  413. <thead valign="bottom">
  414. <tr><th class="head">Option</th>
  415. <th class="head">Meaning</th>
  416. </tr>
  417. </thead>
  418. <tbody valign="top">
  419. <tr><td><tt class="docutils literal"><span class="pre">'+'</span></tt></td>
  420. <td>indicates that a sign should be used for both
  421. positive as well as negative numbers.</td>
  422. </tr>
  423. <tr><td><tt class="docutils literal"><span class="pre">'-'</span></tt></td>
  424. <td>indicates that a sign should be used only for negative
  425. numbers (this is the default behavior).</td>
  426. </tr>
  427. <tr><td>space</td>
  428. <td>indicates that a leading space should be used on
  429. positive numbers, and a minus sign on negative numbers.</td>
  430. </tr>
  431. </tbody>
  432. </table>
  433. </div></blockquote>
  434. <p>The <tt class="docutils literal"><span class="pre">'#'</span></tt> option is only valid for integers, and only for binary, octal, or
  435. hexadecimal output. If present, it specifies that the output will be prefixed
  436. by <tt class="docutils literal"><span class="pre">'0b'</span></tt>, <tt class="docutils literal"><span class="pre">'0o'</span></tt>, or <tt class="docutils literal"><span class="pre">'0x'</span></tt>, respectively.</p>
  437. <p>The <tt class="docutils literal"><span class="pre">','</span></tt> option signals the use of a comma for a thousands separator.
  438. For a locale aware separator, use the <tt class="docutils literal"><span class="pre">'n'</span></tt> integer presentation type
  439. instead.</p>
  440. <p class="versionchanged">
  441. <span class="versionmodified">Changed in version 2.7: </span>Added the <tt class="docutils literal"><span class="pre">','</span></tt> option (see also <span class="target" id="index-3"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0378"><strong>PEP 378</strong></a>).</p>
  442. <p><em>width</em> is a decimal integer defining the minimum field width. If not
  443. specified, then the field width will be determined by the content.</p>
  444. <p>Preceding the <em>width</em> field by a zero (<tt class="docutils literal"><span class="pre">'0'</span></tt>) character enables
  445. sign-aware zero-padding for numeric types. This is equivalent to a <em>fill</em>
  446. character of <tt class="docutils literal"><span class="pre">'0'</span></tt> with an <em>alignment</em> type of <tt class="docutils literal"><span class="pre">'='</span></tt>.</p>
  447. <p>The <em>precision</em> is a decimal number indicating how many digits should be
  448. displayed after the decimal point for a floating point value formatted with
  449. <tt class="docutils literal"><span class="pre">'f'</span></tt> and <tt class="docutils literal"><span class="pre">'F'</span></tt>, or before and after the decimal point for a floating point
  450. value formatted with <tt class="docutils literal"><span class="pre">'g'</span></tt> or <tt class="docutils literal"><span class="pre">'G'</span></tt>. For non-number types the field
  451. indicates the maximum field size - in other words, how many characters will be
  452. used from the field content. The <em>precision</em> is not allowed for integer values.</p>
  453. <p>Finally, the <em>type</em> determines how the data should be presented.</p>
  454. <p>The available string presentation types are:</p>
  455. <blockquote>
  456. <div><table border="1" class="docutils">
  457. <colgroup>
  458. <col width="13%" />
  459. <col width="87%" />
  460. </colgroup>
  461. <thead valign="bottom">
  462. <tr><th class="head">Type</th>
  463. <th class="head">Meaning</th>
  464. </tr>
  465. </thead>
  466. <tbody valign="top">
  467. <tr><td><tt class="docutils literal"><span class="pre">'s'</span></tt></td>
  468. <td>String format. This is the default type for strings and
  469. may be omitted.</td>
  470. </tr>
  471. <tr><td>None</td>
  472. <td>The same as <tt class="docutils literal"><span class="pre">'s'</span></tt>.</td>
  473. </tr>
  474. </tbody>
  475. </table>
  476. </div></blockquote>
  477. <p>The available integer presentation types are:</p>
  478. <blockquote>
  479. <div><table border="1" class="docutils">
  480. <colgroup>
  481. <col width="13%" />
  482. <col width="87%" />
  483. </colgroup>
  484. <thead valign="bottom">
  485. <tr><th class="head">Type</th>
  486. <th class="head">Meaning</th>
  487. </tr>
  488. </thead>
  489. <tbody valign="top">
  490. <tr><td><tt class="docutils literal"><span class="pre">'b'</span></tt></td>
  491. <td>Binary format. Outputs the number in base 2.</td>
  492. </tr>
  493. <tr><td><tt class="docutils literal"><span class="pre">'c'</span></tt></td>
  494. <td>Character. Converts the integer to the corresponding
  495. unicode character before printing.</td>
  496. </tr>
  497. <tr><td><tt class="docutils literal"><span class="pre">'d'</span></tt></td>
  498. <td>Decimal Integer. Outputs the number in base 10.</td>
  499. </tr>
  500. <tr><td><tt class="docutils literal"><span class="pre">'o'</span></tt></td>
  501. <td>Octal format. Outputs the number in base 8.</td>
  502. </tr>
  503. <tr><td><tt class="docutils literal"><span class="pre">'x'</span></tt></td>
  504. <td>Hex format. Outputs the number in base 16, using lower-
  505. case letters for the digits above 9.</td>
  506. </tr>
  507. <tr><td><tt class="docutils literal"><span class="pre">'X'</span></tt></td>
  508. <td>Hex format. Outputs the number in base 16, using upper-
  509. case letters for the digits above 9.</td>
  510. </tr>
  511. <tr><td><tt class="docutils literal"><span class="pre">'n'</span></tt></td>
  512. <td>Number. This is the same as <tt class="docutils literal"><span class="pre">'d'</span></tt>, except that it uses
  513. the current locale setting to insert the appropriate
  514. number separator characters.</td>
  515. </tr>
  516. <tr><td>None</td>
  517. <td>The same as <tt class="docutils literal"><span class="pre">'d'</span></tt>.</td>
  518. </tr>
  519. </tbody>
  520. </table>
  521. </div></blockquote>
  522. <p>In addition to the above presentation types, integers can be formatted
  523. with the floating point presentation types listed below (except
  524. <tt class="docutils literal"><span class="pre">'n'</span></tt> and None). When doing so, <a class="reference internal" href="functions.html#float" title="float"><tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt></a> is used to convert the
  525. integer to a floating point number before formatting.</p>
  526. <p>The available presentation types for floating point and decimal values are:</p>
  527. <blockquote>
  528. <div><table border="1" class="docutils">
  529. <colgroup>
  530. <col width="13%" />
  531. <col width="87%" />
  532. </colgroup>
  533. <thead valign="bottom">
  534. <tr><th class="head">Type</th>
  535. <th class="head">Meaning</th>
  536. </tr>
  537. </thead>
  538. <tbody valign="top">
  539. <tr><td><tt class="docutils literal"><span class="pre">'e'</span></tt></td>
  540. <td>Exponent notation. Prints the number in scientific
  541. notation using the letter &#8216;e&#8217; to indicate the exponent.</td>
  542. </tr>
  543. <tr><td><tt class="docutils literal"><span class="pre">'E'</span></tt></td>
  544. <td>Exponent notation. Same as <tt class="docutils literal"><span class="pre">'e'</span></tt> except it uses an
  545. upper case &#8216;E&#8217; as the separator character.</td>
  546. </tr>
  547. <tr><td><tt class="docutils literal"><span class="pre">'f'</span></tt></td>
  548. <td>Fixed point. Displays the number as a fixed-point
  549. number.</td>
  550. </tr>
  551. <tr><td><tt class="docutils literal"><span class="pre">'F'</span></tt></td>
  552. <td>Fixed point. Same as <tt class="docutils literal"><span class="pre">'f'</span></tt>.</td>
  553. </tr>
  554. <tr><td><tt class="docutils literal"><span class="pre">'g'</span></tt></td>
  555. <td><p class="first">General format. For a given precision <tt class="docutils literal"><span class="pre">p</span> <span class="pre">&gt;=</span> <span class="pre">1</span></tt>,
  556. this rounds the number to <tt class="docutils literal"><span class="pre">p</span></tt> significant digits and
  557. then formats the result in either fixed-point format
  558. or in scientific notation, depending on its magnitude.</p>
  559. <p>The precise rules are as follows: suppose that the
  560. result formatted with presentation type <tt class="docutils literal"><span class="pre">'e'</span></tt> and
  561. precision <tt class="docutils literal"><span class="pre">p-1</span></tt> would have exponent <tt class="docutils literal"><span class="pre">exp</span></tt>. Then
  562. if <tt class="docutils literal"><span class="pre">-4</span> <span class="pre">&lt;=</span> <span class="pre">exp</span> <span class="pre">&lt;</span> <span class="pre">p</span></tt>, the number is formatted
  563. with presentation type <tt class="docutils literal"><span class="pre">'f'</span></tt> and precision
  564. <tt class="docutils literal"><span class="pre">p-1-exp</span></tt>. Otherwise, the number is formatted
  565. with presentation type <tt class="docutils literal"><span class="pre">'e'</span></tt> and precision <tt class="docutils literal"><span class="pre">p-1</span></tt>.
  566. In both cases insignificant trailing zeros are removed
  567. from the significand, and the decimal point is also
  568. removed if there are no remaining digits following it.</p>
  569. <p>Positive and negative infinity, positive and negative
  570. zero, and nans, are formatted as <tt class="docutils literal"><span class="pre">inf</span></tt>, <tt class="docutils literal"><span class="pre">-inf</span></tt>,
  571. <tt class="docutils literal"><span class="pre">0</span></tt>, <tt class="docutils literal"><span class="pre">-0</span></tt> and <tt class="docutils literal"><span class="pre">nan</span></tt> respectively, regardless of
  572. the precision.</p>
  573. <p class="last">A precision of <tt class="docutils literal"><span class="pre">0</span></tt> is treated as equivalent to a
  574. precision of <tt class="docutils literal"><span class="pre">1</span></tt>.</p>
  575. </td>
  576. </tr>
  577. <tr><td><tt class="docutils literal"><span class="pre">'G'</span></tt></td>
  578. <td>General format. Same as <tt class="docutils literal"><span class="pre">'g'</span></tt> except switches to
  579. <tt class="docutils literal"><span class="pre">'E'</span></tt> if the number gets too large. The
  580. representations of infinity and NaN are uppercased, too.</td>
  581. </tr>
  582. <tr><td><tt class="docutils literal"><span class="pre">'n'</span></tt></td>
  583. <td>Number. This is the same as <tt class="docutils literal"><span class="pre">'g'</span></tt>, except that it uses
  584. the current locale setting to insert the appropriate
  585. number separator characters.</td>
  586. </tr>
  587. <tr><td><tt class="docutils literal"><span class="pre">'%'</span></tt></td>
  588. <td>Percentage. Multiplies the number by 100 and displays
  589. in fixed (<tt class="docutils literal"><span class="pre">'f'</span></tt>) format, followed by a percent sign.</td>
  590. </tr>
  591. <tr><td>None</td>
  592. <td>The same as <tt class="docutils literal"><span class="pre">'g'</span></tt>.</td>
  593. </tr>
  594. </tbody>
  595. </table>
  596. </div></blockquote>
  597. </div>
  598. <div class="section" id="format-examples">
  599. <span id="formatexamples"></span><h3>7.1.3.2. Format examples<a class="headerlink" href="#format-examples" title="Permalink to this headline"></a></h3>
  600. <p>This section contains examples of the new format syntax and comparison with
  601. the old <tt class="docutils literal"><span class="pre">%</span></tt>-formatting.</p>
  602. <p>In most of the cases the syntax is similar to the old <tt class="docutils literal"><span class="pre">%</span></tt>-formatting, with the
  603. addition of the <tt class="docutils literal"><span class="pre">{}</span></tt> and with <tt class="docutils literal"><span class="pre">:</span></tt> used instead of <tt class="docutils literal"><span class="pre">%</span></tt>.
  604. For example, <tt class="docutils literal"><span class="pre">'%03.2f'</span></tt> can be translated to <tt class="docutils literal"><span class="pre">'{:03.2f}'</span></tt>.</p>
  605. <p>The new format syntax also supports new and different options, shown in the
  606. follow examples.</p>
  607. <p>Accessing arguments by position:</p>
  608. <div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span