gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

1244 lines
113KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="string — Common string operations" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/string.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/string.py String constants: The constants defined in this module are: Custom String Formatting: The built-in string class provides the ability to do complex variable substitutions ..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/string.py String constants: The constants defined in this module are: Custom String Formatting: The built-in string class provides the ability to do complex variable substitutions ..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>string — Common string operations &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="re — Regular expression operations" href="re.html" />
  33. <link rel="prev" title="Text Processing Services" href="text.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/string.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code> — Common string operations</a><ul>
  86. <li><a class="reference internal" href="#string-constants">String constants</a></li>
  87. <li><a class="reference internal" href="#custom-string-formatting">Custom String Formatting</a></li>
  88. <li><a class="reference internal" href="#format-string-syntax">Format String Syntax</a><ul>
  89. <li><a class="reference internal" href="#format-specification-mini-language">Format Specification Mini-Language</a></li>
  90. <li><a class="reference internal" href="#format-examples">Format examples</a></li>
  91. </ul>
  92. </li>
  93. <li><a class="reference internal" href="#template-strings">Template strings</a></li>
  94. <li><a class="reference internal" href="#helper-functions">Helper functions</a></li>
  95. </ul>
  96. </li>
  97. </ul>
  98. </div>
  99. <div>
  100. <h4>Previous topic</h4>
  101. <p class="topless"><a href="text.html"
  102. title="previous chapter">Text Processing Services</a></p>
  103. </div>
  104. <div>
  105. <h4>Next topic</h4>
  106. <p class="topless"><a href="re.html"
  107. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code> — Regular expression operations</a></p>
  108. </div>
  109. <div role="note" aria-label="source link">
  110. <h3>This Page</h3>
  111. <ul class="this-page-menu">
  112. <li><a href="../bugs.html">Report a Bug</a></li>
  113. <li>
  114. <a href="https://github.com/python/cpython/blob/main/Doc/library/string.rst"
  115. rel="nofollow">Show Source
  116. </a>
  117. </li>
  118. </ul>
  119. </div>
  120. </nav>
  121. </div>
  122. </div>
  123. <div class="related" role="navigation" aria-label="related navigation">
  124. <h3>Navigation</h3>
  125. <ul>
  126. <li class="right" style="margin-right: 10px">
  127. <a href="../genindex.html" title="General Index"
  128. accesskey="I">index</a></li>
  129. <li class="right" >
  130. <a href="../py-modindex.html" title="Python Module Index"
  131. >modules</a> |</li>
  132. <li class="right" >
  133. <a href="re.html" title="re — Regular expression operations"
  134. accesskey="N">next</a> |</li>
  135. <li class="right" >
  136. <a href="text.html" title="Text Processing Services"
  137. accesskey="P">previous</a> |</li>
  138. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  139. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  140. <li class="switchers">
  141. <div class="language_switcher_placeholder"></div>
  142. <div class="version_switcher_placeholder"></div>
  143. </li>
  144. <li>
  145. </li>
  146. <li id="cpython-language-and-version">
  147. <a href="../index.html">3.12.3 Documentation</a> &#187;
  148. </li>
  149. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  150. <li class="nav-item nav-item-2"><a href="text.html" accesskey="U">Text Processing Services</a> &#187;</li>
  151. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code> — Common string operations</a></li>
  152. <li class="right">
  153. <div class="inline-search" role="search">
  154. <form class="inline-search" action="../search.html" method="get">
  155. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  156. <input type="submit" value="Go" />
  157. </form>
  158. </div>
  159. |
  160. </li>
  161. <li class="right">
  162. <label class="theme-selector-label">
  163. Theme
  164. <select class="theme-selector" oninput="activateTheme(this.value)">
  165. <option value="auto" selected>Auto</option>
  166. <option value="light">Light</option>
  167. <option value="dark">Dark</option>
  168. </select>
  169. </label> |</li>
  170. </ul>
  171. </div>
  172. <div class="document">
  173. <div class="documentwrapper">
  174. <div class="bodywrapper">
  175. <div class="body" role="main">
  176. <section id="module-string">
  177. <span id="string-common-string-operations"></span><h1><a class="reference internal" href="#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> — Common string operations<a class="headerlink" href="#module-string" title="Link to this heading">¶</a></h1>
  178. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/string.py">Lib/string.py</a></p>
  179. <hr class="docutils" />
  180. <div class="admonition seealso">
  181. <p class="admonition-title">See also</p>
  182. <p><a class="reference internal" href="stdtypes.html#textseq"><span class="std std-ref">Text Sequence Type — str</span></a></p>
  183. <p><a class="reference internal" href="stdtypes.html#string-methods"><span class="std std-ref">String Methods</span></a></p>
  184. </div>
  185. <section id="string-constants">
  186. <h2>String constants<a class="headerlink" href="#string-constants" title="Link to this heading">¶</a></h2>
  187. <p>The constants defined in this module are:</p>
  188. <dl class="py data">
  189. <dt class="sig sig-object py" id="string.ascii_letters">
  190. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">ascii_letters</span></span><a class="headerlink" href="#string.ascii_letters" title="Link to this definition">¶</a></dt>
  191. <dd><p>The concatenation of the <a class="reference internal" href="#string.ascii_lowercase" title="string.ascii_lowercase"><code class="xref py py-const docutils literal notranslate"><span class="pre">ascii_lowercase</span></code></a> and <a class="reference internal" href="#string.ascii_uppercase" title="string.ascii_uppercase"><code class="xref py py-const docutils literal notranslate"><span class="pre">ascii_uppercase</span></code></a>
  192. constants described below. This value is not locale-dependent.</p>
  193. </dd></dl>
  194. <dl class="py data">
  195. <dt class="sig sig-object py" id="string.ascii_lowercase">
  196. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">ascii_lowercase</span></span><a class="headerlink" href="#string.ascii_lowercase" title="Link to this definition">¶</a></dt>
  197. <dd><p>The lowercase letters <code class="docutils literal notranslate"><span class="pre">'abcdefghijklmnopqrstuvwxyz'</span></code>. This value is not
  198. locale-dependent and will not change.</p>
  199. </dd></dl>
  200. <dl class="py data">
  201. <dt class="sig sig-object py" id="string.ascii_uppercase">
  202. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">ascii_uppercase</span></span><a class="headerlink" href="#string.ascii_uppercase" title="Link to this definition">¶</a></dt>
  203. <dd><p>The uppercase letters <code class="docutils literal notranslate"><span class="pre">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span></code>. This value is not
  204. locale-dependent and will not change.</p>
  205. </dd></dl>
  206. <dl class="py data">
  207. <dt class="sig sig-object py" id="string.digits">
  208. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">digits</span></span><a class="headerlink" href="#string.digits" title="Link to this definition">¶</a></dt>
  209. <dd><p>The string <code class="docutils literal notranslate"><span class="pre">'0123456789'</span></code>.</p>
  210. </dd></dl>
  211. <dl class="py data">
  212. <dt class="sig sig-object py" id="string.hexdigits">
  213. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">hexdigits</span></span><a class="headerlink" href="#string.hexdigits" title="Link to this definition">¶</a></dt>
  214. <dd><p>The string <code class="docutils literal notranslate"><span class="pre">'0123456789abcdefABCDEF'</span></code>.</p>
  215. </dd></dl>
  216. <dl class="py data">
  217. <dt class="sig sig-object py" id="string.octdigits">
  218. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">octdigits</span></span><a class="headerlink" href="#string.octdigits" title="Link to this definition">¶</a></dt>
  219. <dd><p>The string <code class="docutils literal notranslate"><span class="pre">'01234567'</span></code>.</p>
  220. </dd></dl>
  221. <dl class="py data">
  222. <dt class="sig sig-object py" id="string.punctuation">
  223. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">punctuation</span></span><a class="headerlink" href="#string.punctuation" title="Link to this definition">¶</a></dt>
  224. <dd><p>String of ASCII characters which are considered punctuation characters
  225. in the <code class="docutils literal notranslate"><span class="pre">C</span></code> locale: <code class="docutils literal notranslate"><span class="pre">!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?&#64;[\]^_`{|}~</span></code>.</p>
  226. </dd></dl>
  227. <dl class="py data">
  228. <dt class="sig sig-object py" id="string.printable">
  229. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">printable</span></span><a class="headerlink" href="#string.printable" title="Link to this definition">¶</a></dt>
  230. <dd><p>String of ASCII characters which are considered printable. This is a
  231. combination of <a class="reference internal" href="#string.digits" title="string.digits"><code class="xref py py-const docutils literal notranslate"><span class="pre">digits</span></code></a>, <a class="reference internal" href="#string.ascii_letters" title="string.ascii_letters"><code class="xref py py-const docutils literal notranslate"><span class="pre">ascii_letters</span></code></a>, <a class="reference internal" href="#string.punctuation" title="string.punctuation"><code class="xref py py-const docutils literal notranslate"><span class="pre">punctuation</span></code></a>,
  232. and <a class="reference internal" href="#string.whitespace" title="string.whitespace"><code class="xref py py-const docutils literal notranslate"><span class="pre">whitespace</span></code></a>.</p>
  233. </dd></dl>
  234. <dl class="py data">
  235. <dt class="sig sig-object py" id="string.whitespace">
  236. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">whitespace</span></span><a class="headerlink" href="#string.whitespace" title="Link to this definition">¶</a></dt>
  237. <dd><p>A string containing all ASCII characters that are considered whitespace.
  238. This includes the characters space, tab, linefeed, return, formfeed, and
  239. vertical tab.</p>
  240. </dd></dl>
  241. </section>
  242. <section id="custom-string-formatting">
  243. <span id="string-formatting"></span><h2>Custom String Formatting<a class="headerlink" href="#custom-string-formatting" title="Link to this heading">¶</a></h2>
  244. <p>The built-in string class provides the ability to do complex variable
  245. substitutions and value formatting via the <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method described in
  246. <span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-3101/"><strong>PEP 3101</strong></a>. The <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> class in the <a class="reference internal" href="#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module allows
  247. you to create and customize your own string formatting behaviors using the same
  248. implementation as the built-in <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format()</span></code></a> method.</p>
  249. <dl class="py class">
  250. <dt class="sig sig-object py" id="string.Formatter">
  251. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">Formatter</span></span><a class="headerlink" href="#string.Formatter" title="Link to this definition">¶</a></dt>
  252. <dd><p>The <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> class has the following public methods:</p>
  253. <dl class="py method">
  254. <dt class="sig sig-object py" id="string.Formatter.format">
  255. <span class="sig-name descname"><span class="pre">format</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format_string</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.format" title="Link to this definition">¶</a></dt>
  256. <dd><p>The primary API method. It takes a format string and
  257. an arbitrary set of positional and keyword arguments.
  258. It is just a wrapper that calls <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a>.</p>
  259. <div class="versionchanged">
  260. <p><span class="versionmodified changed">Changed in version 3.7: </span>A format string argument is now <a class="reference internal" href="../glossary.html#positional-only-parameter"><span class="std std-ref">positional-only</span></a>.</p>
  261. </div>
  262. </dd></dl>
  263. <dl class="py method">
  264. <dt class="sig sig-object py" id="string.Formatter.vformat">
  265. <span class="sig-name descname"><span class="pre">vformat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format_string</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.vformat" title="Link to this definition">¶</a></dt>
  266. <dd><p>This function does the actual work of formatting. It is exposed as a
  267. separate function for cases where you want to pass in a predefined
  268. dictionary of arguments, rather than unpacking and repacking the
  269. dictionary as individual arguments using the <code class="docutils literal notranslate"><span class="pre">*args</span></code> and <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>
  270. syntax. <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a> does the work of breaking up the format string
  271. into character data and replacement fields. It calls the various
  272. methods described below.</p>
  273. </dd></dl>
  274. <p>In addition, the <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> defines a number of methods that are
  275. intended to be replaced by subclasses:</p>
  276. <dl class="py method">
  277. <dt class="sig sig-object py" id="string.Formatter.parse">
  278. <span class="sig-name descname"><span class="pre">parse</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format_string</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.parse" title="Link to this definition">¶</a></dt>
  279. <dd><p>Loop over the format_string and return an iterable of tuples
  280. (<em>literal_text</em>, <em>field_name</em>, <em>format_spec</em>, <em>conversion</em>). This is used
  281. by <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a> to break the string into either literal text, or
  282. replacement fields.</p>
  283. <p>The values in the tuple conceptually represent a span of literal text
  284. followed by a single replacement field. If there is no literal text
  285. (which can happen if two replacement fields occur consecutively), then
  286. <em>literal_text</em> will be a zero-length string. If there is no replacement
  287. field, then the values of <em>field_name</em>, <em>format_spec</em> and <em>conversion</em>
  288. will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  289. </dd></dl>
  290. <dl class="py method">
  291. <dt class="sig sig-object py" id="string.Formatter.get_field">
  292. <span class="sig-name descname"><span class="pre">get_field</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">field_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.get_field" title="Link to this definition">¶</a></dt>
  293. <dd><p>Given <em>field_name</em> as returned by <a class="reference internal" href="#string.Formatter.parse" title="string.Formatter.parse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse()</span></code></a> (see above), convert it to
  294. an object to be formatted. Returns a tuple (obj, used_key). The default
  295. version takes strings of the form defined in <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-3101/"><strong>PEP 3101</strong></a>, such as
  296. “0[name]” or “label.title”. <em>args</em> and <em>kwargs</em> are as passed in to
  297. <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a>. The return value <em>used_key</em> has the same meaning as the
  298. <em>key</em> parameter to <a class="reference internal" href="#string.Formatter.get_value" title="string.Formatter.get_value"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_value()</span></code></a>.</p>
  299. </dd></dl>
  300. <dl class="py method">
  301. <dt class="sig sig-object py" id="string.Formatter.get_value">
  302. <span class="sig-name descname"><span class="pre">get_value</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.get_value" title="Link to this definition">¶</a></dt>
  303. <dd><p>Retrieve a given field value. The <em>key</em> argument will be either an
  304. integer or a string. If it is an integer, it represents the index of the
  305. positional argument in <em>args</em>; if it is a string, then it represents a
  306. named argument in <em>kwargs</em>.</p>
  307. <p>The <em>args</em> parameter is set to the list of positional arguments to
  308. <a class="reference internal" href="#string.Formatter.vformat" title="string.Formatter.vformat"><code class="xref py py-meth docutils literal notranslate"><span class="pre">vformat()</span></code></a>, and the <em>kwargs</em> parameter is set to the dictionary of
  309. keyword arguments.</p>
  310. <p>For compound field names, these functions are only called for the first
  311. component of the field name; subsequent components are handled through
  312. normal attribute and indexing operations.</p>
  313. <p>So for example, the field expression ‘0.name’ would cause
  314. <a class="reference internal" href="#string.Formatter.get_value" title="string.Formatter.get_value"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_value()</span></code></a> to be called with a <em>key</em> argument of 0. The <code class="docutils literal notranslate"><span class="pre">name</span></code>
  315. attribute will be looked up after <a class="reference internal" href="#string.Formatter.get_value" title="string.Formatter.get_value"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_value()</span></code></a> returns by calling the
  316. built-in <a class="reference internal" href="functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> function.</p>
  317. <p>If the index or keyword refers to an item that does not exist, then an
  318. <a class="reference internal" href="exceptions.html#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a> or <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> should be raised.</p>
  319. </dd></dl>
  320. <dl class="py method">
  321. <dt class="sig sig-object py" id="string.Formatter.check_unused_args">
  322. <span class="sig-name descname"><span class="pre">check_unused_args</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">used_args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.check_unused_args" title="Link to this definition">¶</a></dt>
  323. <dd><p>Implement checking for unused arguments if desired. The arguments to this
  324. function is the set of all argument keys that were actually referred to in
  325. the format string (integers for positional arguments, and strings for
  326. named arguments), and a reference to the <em>args</em> and <em>kwargs</em> that was
  327. passed to vformat. The set of unused args can be calculated from these
  328. parameters. <a class="reference internal" href="#string.Formatter.check_unused_args" title="string.Formatter.check_unused_args"><code class="xref py py-meth docutils literal notranslate"><span class="pre">check_unused_args()</span></code></a> is assumed to raise an exception if
  329. the check fails.</p>
  330. </dd></dl>
  331. <dl class="py method">
  332. <dt class="sig sig-object py" id="string.Formatter.format_field">
  333. <span class="sig-name descname"><span class="pre">format_field</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">format_spec</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.format_field" title="Link to this definition">¶</a></dt>
  334. <dd><p><a class="reference internal" href="#string.Formatter.format_field" title="string.Formatter.format_field"><code class="xref py py-meth docutils literal notranslate"><span class="pre">format_field()</span></code></a> simply calls the global <a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> built-in. The
  335. method is provided so that subclasses can override it.</p>
  336. </dd></dl>
  337. <dl class="py method">
  338. <dt class="sig sig-object py" id="string.Formatter.convert_field">
  339. <span class="sig-name descname"><span class="pre">convert_field</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">conversion</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Formatter.convert_field" title="Link to this definition">¶</a></dt>
  340. <dd><p>Converts the value (returned by <a class="reference internal" href="#string.Formatter.get_field" title="string.Formatter.get_field"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_field()</span></code></a>) given a conversion type
  341. (as in the tuple returned by the <a class="reference internal" href="#string.Formatter.parse" title="string.Formatter.parse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse()</span></code></a> method). The default
  342. version understands ‘s’ (str), ‘r’ (repr) and ‘a’ (ascii) conversion
  343. types.</p>
  344. </dd></dl>
  345. </dd></dl>
  346. </section>
  347. <section id="format-string-syntax">
  348. <span id="formatstrings"></span><h2>Format String Syntax<a class="headerlink" href="#format-string-syntax" title="Link to this heading">¶</a></h2>
  349. <p>The <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> method and the <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a> class share the same
  350. syntax for format strings (although in the case of <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a>,
  351. subclasses can define their own format string syntax). The syntax is
  352. related to that of <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">formatted string literals</span></a>, but it is
  353. less sophisticated and, in particular, does not support arbitrary expressions.</p>
  354. <p id="index-2">Format strings contain “replacement fields” surrounded by curly braces <code class="docutils literal notranslate"><span class="pre">{}</span></code>.
  355. Anything that is not contained in braces is considered literal text, which is
  356. copied unchanged to the output. If you need to include a brace character in the
  357. literal text, it can be escaped by doubling: <code class="docutils literal notranslate"><span class="pre">{{</span></code> and <code class="docutils literal notranslate"><span class="pre">}}</span></code>.</p>
  358. <p>The grammar for a replacement field is as follows:</p>
  359. <pre>
  360. <strong id="grammar-token-format-string-replacement_field">replacement_field</strong> ::= &quot;{&quot; [<a class="reference internal" href="#grammar-token-format-string-field_name"><code class="xref docutils literal notranslate"><span class="pre">field_name</span></code></a>] [&quot;!&quot; <a class="reference internal" href="#grammar-token-format-string-conversion"><code class="xref docutils literal notranslate"><span class="pre">conversion</span></code></a>] [&quot;:&quot; <a class="reference internal" href="#grammar-token-format-string-format_spec"><code class="xref docutils literal notranslate"><span class="pre">format_spec</span></code></a>] &quot;}&quot;
  361. <strong id="grammar-token-format-string-field_name">field_name </strong> ::= <a class="reference internal" href="#grammar-token-format-string-arg_name"><code class="xref docutils literal notranslate"><span class="pre">arg_name</span></code></a> (&quot;.&quot; <a class="reference internal" href="#grammar-token-format-string-attribute_name"><code class="xref docutils literal notranslate"><span class="pre">attribute_name</span></code></a> | &quot;[&quot; <a class="reference internal" href="#grammar-token-format-string-element_index"><code class="xref docutils literal notranslate"><span class="pre">element_index</span></code></a> &quot;]&quot;)*
  362. <strong id="grammar-token-format-string-arg_name">arg_name </strong> ::= [<a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> | <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>+]
  363. <strong id="grammar-token-format-string-attribute_name">attribute_name </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-python-grammar-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>
  364. <strong id="grammar-token-format-string-element_index">element_index </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>+ | <a class="reference internal" href="#grammar-token-format-string-index_string"><code class="xref docutils literal notranslate"><span class="pre">index_string</span></code></a>
  365. <strong id="grammar-token-format-string-index_string">index_string </strong> ::= &lt;any source character except &quot;]&quot;&gt; +
  366. <strong id="grammar-token-format-string-conversion">conversion </strong> ::= &quot;r&quot; | &quot;s&quot; | &quot;a&quot;
  367. <strong id="grammar-token-format-string-format_spec">format_spec </strong> ::= <a class="reference internal" href="#grammar-token-format-spec-format_spec"><code class="xref docutils literal notranslate"><span class="pre">format-spec:format_spec</span></code></a>
  368. </pre>
  369. <p>In less formal terms, the replacement field can start with a <em>field_name</em> that specifies
  370. the object whose value is to be formatted and inserted
  371. into the output instead of the replacement field.
  372. The <em>field_name</em> is optionally followed by a <em>conversion</em> field, which is
  373. preceded by an exclamation point <code class="docutils literal notranslate"><span class="pre">'!'</span></code>, and a <em>format_spec</em>, which is preceded
  374. by a colon <code class="docutils literal notranslate"><span class="pre">':'</span></code>. These specify a non-default format for the replacement value.</p>
  375. <p>See also the <a class="reference internal" href="#formatspec"><span class="std std-ref">Format Specification Mini-Language</span></a> section.</p>
  376. <p>The <em>field_name</em> itself begins with an <em>arg_name</em> that is either a number or a
  377. keyword. If it’s a number, it refers to a positional argument, and if it’s a keyword,
  378. it refers to a named keyword argument. An <em>arg_name</em> is treated as a number if
  379. a call to <a class="reference internal" href="stdtypes.html#str.isdecimal" title="str.isdecimal"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.isdecimal()</span></code></a> on the string would return true.
  380. If the numerical arg_names in a format string
  381. are 0, 1, 2, … in sequence, they can all be omitted (not just some)
  382. and the numbers 0, 1, 2, … will be automatically inserted in that order.
  383. Because <em>arg_name</em> is not quote-delimited, it is not possible to specify arbitrary
  384. dictionary keys (e.g., the strings <code class="docutils literal notranslate"><span class="pre">'10'</span></code> or <code class="docutils literal notranslate"><span class="pre">':-]'</span></code>) within a format string.
  385. The <em>arg_name</em> can be followed by any number of index or
  386. attribute expressions. An expression of the form <code class="docutils literal notranslate"><span class="pre">'.name'</span></code> selects the named
  387. attribute using <a class="reference internal" href="functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a>, while an expression of the form <code class="docutils literal notranslate"><span class="pre">'[index]'</span></code>
  388. does an index lookup using <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a>.</p>
  389. <div class="versionchanged">
  390. <p><span class="versionmodified changed">Changed in version 3.1: </span>The positional argument specifiers can be omitted for <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>,
  391. so <code class="docutils literal notranslate"><span class="pre">'{}</span> <span class="pre">{}'.format(a,</span> <span class="pre">b)</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">'{0}</span> <span class="pre">{1}'.format(a,</span> <span class="pre">b)</span></code>.</p>
  392. </div>
  393. <div class="versionchanged">
  394. <p><span class="versionmodified changed">Changed in version 3.4: </span>The positional argument specifiers can be omitted for <a class="reference internal" href="#string.Formatter" title="string.Formatter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Formatter</span></code></a>.</p>
  395. </div>
  396. <p>Some simple format string examples:</p>
  397. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="s2">&quot;First, thou shalt count to </span><span class="si">{0}</span><span class="s2">&quot;</span> <span class="c1"># References first positional argument</span>
  398. <span class="s2">&quot;Bring me a </span><span class="si">{}</span><span class="s2">&quot;</span> <span class="c1"># Implicitly references the first positional argument</span>
  399. <span class="s2">&quot;From </span><span class="si">{}</span><span class="s2"> to </span><span class="si">{}</span><span class="s2">&quot;</span> <span class="c1"># Same as &quot;From {0} to {1}&quot;</span>
  400. <span class="s2">&quot;My quest is </span><span class="si">{name}</span><span class="s2">&quot;</span> <span class="c1"># References keyword argument &#39;name&#39;</span>
  401. <span class="s2">&quot;Weight in tons </span><span class="si">{0.weight}</span><span class="s2">&quot;</span> <span class="c1"># &#39;weight&#39; attribute of first positional arg</span>
  402. <span class="s2">&quot;Units destroyed: </span><span class="si">{players[0]}</span><span class="s2">&quot;</span> <span class="c1"># First element of keyword argument &#39;players&#39;.</span>
  403. </pre></div>
  404. </div>
  405. <p>The <em>conversion</em> field causes a type coercion before formatting. Normally, the
  406. job of formatting a value is done by the <a class="reference internal" href="../reference/datamodel.html#object.__format__" title="object.__format__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__format__()</span></code></a> method of the value
  407. itself. However, in some cases it is desirable to force a type to be formatted
  408. as a string, overriding its own definition of formatting. By converting the
  409. value to a string before calling <a class="reference internal" href="../reference/datamodel.html#object.__format__" title="object.__format__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__format__()</span></code></a>, the normal formatting logic
  410. is bypassed.</p>
  411. <p>Three conversion flags are currently supported: <code class="docutils literal notranslate"><span class="pre">'!s'</span></code> which calls <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>
  412. on the value, <code class="docutils literal notranslate"><span class="pre">'!r'</span></code> which calls <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> and <code class="docutils literal notranslate"><span class="pre">'!a'</span></code> which calls
  413. <a class="reference internal" href="functions.html#ascii" title="ascii"><code class="xref py py-func docutils literal notranslate"><span class="pre">ascii()</span></code></a>.</p>
  414. <p>Some examples:</p>
  415. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="s2">&quot;Harold&#39;s a clever </span><span class="si">{0!s}</span><span class="s2">&quot;</span> <span class="c1"># Calls str() on the argument first</span>
  416. <span class="s2">&quot;Bring out the holy </span><span class="si">{name!r}</span><span class="s2">&quot;</span> <span class="c1"># Calls repr() on the argument first</span>
  417. <span class="s2">&quot;More </span><span class="si">{!a}</span><span class="s2">&quot;</span> <span class="c1"># Calls ascii() on the argument first</span>
  418. </pre></div>
  419. </div>
  420. <p>The <em>format_spec</em> field contains a specification of how the value should be
  421. presented, including such details as field width, alignment, padding, decimal
  422. precision and so on. Each value type can define its own “formatting
  423. mini-language” or interpretation of the <em>format_spec</em>.</p>
  424. <p>Most built-in types support a common formatting mini-language, which is
  425. described in the next section.</p>
  426. <p>A <em>format_spec</em> field can also include nested replacement fields within it.
  427. These nested replacement fields may contain a field name, conversion flag
  428. and format specification, but deeper nesting is
  429. not allowed. The replacement fields within the
  430. format_spec are substituted before the <em>format_spec</em> string is interpreted.
  431. This allows the formatting of a value to be dynamically specified.</p>
  432. <p>See the <a class="reference internal" href="#formatexamples"><span class="std std-ref">Format examples</span></a> section for some examples.</p>
  433. <section id="format-specification-mini-language">
  434. <span id="formatspec"></span><h3>Format Specification Mini-Language<a class="headerlink" href="#format-specification-mini-language" title="Link to this heading">¶</a></h3>
  435. <p>“Format specifications” are used within replacement fields contained within a
  436. format string to define how individual values are presented (see
  437. <a class="reference internal" href="#formatstrings"><span class="std std-ref">Format String Syntax</span></a> and <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">f-strings</span></a>).
  438. They can also be passed directly to the built-in
  439. <a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> function. Each formattable type may define how the format
  440. specification is to be interpreted.</p>
  441. <p>Most built-in types implement the following options for format specifications,
  442. although some of the formatting options are only supported by the numeric types.</p>
  443. <p>A general convention is that an empty format specification produces
  444. the same result as if you had called <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> on the value. A
  445. non-empty format specification typically modifies the result.</p>
  446. <p>The general form of a <em>standard format specifier</em> is:</p>
  447. <pre>
  448. <strong id="grammar-token-format-spec-format_spec">format_spec </strong> ::= [[<a class="reference internal" href="#grammar-token-format-spec-fill"><code class="xref docutils literal notranslate"><span class="pre">fill</span></code></a>]<a class="reference internal" href="#grammar-token-format-spec-align"><code class="xref docutils literal notranslate"><span class="pre">align</span></code></a>][<a class="reference internal" href="#grammar-token-format-spec-sign"><code class="xref docutils literal notranslate"><span class="pre">sign</span></code></a>][&quot;z&quot;][&quot;#&quot;][&quot;0&quot;][<a class="reference internal" href="#grammar-token-format-spec-width"><code class="xref docutils literal notranslate"><span class="pre">width</span></code></a>][<a class="reference internal" href="#grammar-token-format-spec-grouping_option"><code class="xref docutils literal notranslate"><span class="pre">grouping_option</span></code></a>][&quot;.&quot; <a class="reference internal" href="#grammar-token-format-spec-precision"><code class="xref docutils literal notranslate"><span class="pre">precision</span></code></a>][<a class="reference internal" href="#grammar-token-format-spec-type"><code class="xref docutils literal notranslate"><span class="pre">type</span></code></a>]
  449. <strong id="grammar-token-format-spec-fill">fill </strong> ::= &lt;any character&gt;
  450. <strong id="grammar-token-format-spec-align">align </strong> ::= &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;=&quot; | &quot;^&quot;
  451. <strong id="grammar-token-format-spec-sign">sign </strong> ::= &quot;+&quot; | &quot;-&quot; | &quot; &quot;
  452. <strong id="grammar-token-format-spec-width">width </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>+
  453. <strong id="grammar-token-format-spec-grouping_option">grouping_option</strong> ::= &quot;_&quot; | &quot;,&quot;
  454. <strong id="grammar-token-format-spec-precision">precision </strong> ::= <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>+
  455. <strong id="grammar-token-format-spec-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;
  456. </pre>
  457. <p>If a valid <em>align</em> value is specified, it can be preceded by a <em>fill</em>
  458. character that can be any character and defaults to a space if omitted.
  459. It is not possible to use a literal curly brace (”<code class="docutils literal notranslate"><span class="pre">{</span></code>” or “<code class="docutils literal notranslate"><span class="pre">}</span></code>”) as
  460. the <em>fill</em> character in a <a class="reference internal" href="../reference/lexical_analysis.html#f-strings"><span class="std std-ref">formatted string literal</span></a> or when using the <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>
  461. method. However, it is possible to insert a curly brace
  462. with a nested replacement field. This limitation doesn’t
  463. affect the <a class="reference internal" href="functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> function.</p>
  464. <p>The meaning of the various alignment options is as follows:</p>
  465. <table class="docutils align-default" id="index-3">
  466. <thead>
  467. <tr class="row-odd"><th class="head"><p>Option</p></th>
  468. <th class="head"><p>Meaning</p></th>
  469. </tr>
  470. </thead>
  471. <tbody>
  472. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'&lt;'</span></code></p></td>
  473. <td><p>Forces the field to be left-aligned within the available
  474. space (this is the default for most objects).</p></td>
  475. </tr>
  476. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'&gt;'</span></code></p></td>
  477. <td><p>Forces the field to be right-aligned within the
  478. available space (this is the default for numbers).</p></td>
  479. </tr>
  480. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'='</span></code></p></td>
  481. <td><p>Forces the padding to be placed after the sign (if any)
  482. but before the digits. This is used for printing fields
  483. in the form ‘+000000120’. This alignment option is only
  484. valid for numeric types. It becomes the default for
  485. numbers when ‘0’ immediately precedes the field width.</p></td>
  486. </tr>
  487. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'^'</span></code></p></td>
  488. <td><p>Forces the field to be centered within the available
  489. space.</p></td>
  490. </tr>
  491. </tbody>
  492. </table>
  493. <p>Note that unless a minimum field width is defined, the field width will always
  494. be the same size as the data to fill it, so that the alignment option has no
  495. meaning in this case.</p>
  496. <p>The <em>sign</em> option is only valid for number types, and can be one of the
  497. following:</p>
  498. <table class="docutils align-default" id="index-4">
  499. <thead>
  500. <tr class="row-odd"><th class="head"><p>Option</p></th>
  501. <th class="head"><p>Meaning</p></th>
  502. </tr>
  503. </thead>
  504. <tbody>
  505. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'+'</span></code></p></td>
  506. <td><p>indicates that a sign should be used for both
  507. positive as well as negative numbers.</p></td>
  508. </tr>
  509. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'-'</span></code></p></td>
  510. <td><p>indicates that a sign should be used only for negative
  511. numbers (this is the default behavior).</p></td>
  512. </tr>
  513. <tr class="row-even"><td><p>space</p></td>
  514. <td><p>indicates that a leading space should be used on
  515. positive numbers, and a minus sign on negative numbers.</p></td>
  516. </tr>
  517. </tbody>
  518. </table>
  519. <p id="index-5">The <code class="docutils literal notranslate"><span class="pre">'z'</span></code> option coerces negative zero floating-point values to positive
  520. zero after rounding to the format precision. This option is only valid for
  521. floating-point presentation types.</p>
  522. <div class="versionchanged">
  523. <p><span class="versionmodified changed">Changed in version 3.11: </span>Added the <code class="docutils literal notranslate"><span class="pre">'z'</span></code> option (see also <span class="target" id="index-6"></span><a class="pep reference external" href="https://peps.python.org/pep-0682/"><strong>PEP 682</strong></a>).</p>
  524. </div>
  525. <p id="index-7">The <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option causes the “alternate form” to be used for the
  526. conversion. The alternate form is defined differently for different
  527. types. This option is only valid for integer, float and complex
  528. types. For integers, when binary, octal, or hexadecimal output
  529. is used, this option adds the respective prefix <code class="docutils literal notranslate"><span class="pre">'0b'</span></code>, <code class="docutils literal notranslate"><span class="pre">'0o'</span></code>,
  530. <code class="docutils literal notranslate"><span class="pre">'0x'</span></code>, or <code class="docutils literal notranslate"><span class="pre">'0X'</span></code> to the output value. For float and complex the
  531. alternate form causes the result of the conversion to always contain a
  532. decimal-point character, even if no digits follow it. Normally, a
  533. decimal-point character appears in the result of these conversions
  534. only if a digit follows it. In addition, for <code class="docutils literal notranslate"><span class="pre">'g'</span></code> and <code class="docutils literal notranslate"><span class="pre">'G'</span></code>
  535. conversions, trailing zeros are not removed from the result.</p>
  536. <p id="index-8">The <code class="docutils literal notranslate"><span class="pre">','</span></code> option signals the use of a comma for a thousands separator.
  537. For a locale aware separator, use the <code class="docutils literal notranslate"><span class="pre">'n'</span></code> integer presentation type
  538. instead.</p>
  539. <div class="versionchanged">
  540. <p><span class="versionmodified changed">Changed in version 3.1: </span>Added the <code class="docutils literal notranslate"><span class="pre">','</span></code> option (see also <span class="target" id="index-9"></span><a class="pep reference external" href="https://peps.python.org/pep-0378/"><strong>PEP 378</strong></a>).</p>
  541. </div>
  542. <p id="index-10">The <code class="docutils literal notranslate"><span class="pre">'_'</span></code> option signals the use of an underscore for a thousands
  543. separator for floating point presentation types and for integer
  544. presentation type <code class="docutils literal notranslate"><span class="pre">'d'</span></code>. For integer presentation types <code class="docutils literal notranslate"><span class="pre">'b'</span></code>,
  545. <code class="docutils literal notranslate"><span class="pre">'o'</span></code>, <code class="docutils literal notranslate"><span class="pre">'x'</span></code>, and <code class="docutils literal notranslate"><span class="pre">'X'</span></code>, underscores will be inserted every 4
  546. digits. For other presentation types, specifying this option is an
  547. error.</p>
  548. <div class="versionchanged">
  549. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added the <code class="docutils literal notranslate"><span class="pre">'_'</span></code> option (see also <span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-0515/"><strong>PEP 515</strong></a>).</p>
  550. </div>
  551. <p><em>width</em> is a decimal integer defining the minimum total field width,
  552. including any prefixes, separators, and other formatting characters.
  553. If not specified, then the field width will be determined by the content.</p>
  554. <p>When no explicit alignment is given, preceding the <em>width</em> field by a zero
  555. (<code class="docutils literal notranslate"><span class="pre">'0'</span></code>) character enables
  556. sign-aware zero-padding for numeric types. This is equivalent to a <em>fill</em>
  557. character of <code class="docutils literal notranslate"><span class="pre">'0'</span></code> with an <em>alignment</em> type of <code class="docutils literal notranslate"><span class="pre">'='</span></code>.</p>
  558. <div class="versionchanged">
  559. <p><span class="versionmodified changed">Changed in version 3.10: </span>Preceding the <em>width</em> field by <code class="docutils literal notranslate"><span class="pre">'0'</span></code> no longer affects the default
  560. alignment for strings.</p>
  561. </div>
  562. <p>The <em>precision</em> is a decimal integer indicating how many digits should be
  563. displayed after the decimal point for presentation types
  564. <code class="docutils literal notranslate"><span class="pre">'f'</span></code> and <code class="docutils literal notranslate"><span class="pre">'F'</span></code>, or before and after the decimal point for presentation
  565. types <code class="docutils literal notranslate"><span class="pre">'g'</span></code> or <code class="docutils literal notranslate"><span class="pre">'G'</span></code>. For string presentation types the field
  566. indicates the maximum field size - in other words, how many characters will be
  567. used from the field content. The <em>precision</em> is not allowed for integer
  568. presentation types.</p>
  569. <p>Finally, the <em>type</em> determines how the data should be presented.</p>
  570. <p>The available string presentation types are:</p>
  571. <blockquote>
  572. <div><table class="docutils align-default">
  573. <thead>
  574. <tr class="row-odd"><th class="head"><p>Type</p></th>
  575. <th class="head"><p>Meaning</p></th>
  576. </tr>
  577. </thead>
  578. <tbody>
  579. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'s'</span></code></p></td>
  580. <td><p>String format. This is the default type for strings and
  581. may be omitted.</p></td>
  582. </tr>
  583. <tr class="row-odd"><td><p>None</p></td>
  584. <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'s'</span></code>.</p></td>
  585. </tr>
  586. </tbody>
  587. </table>
  588. </div></blockquote>
  589. <p>The available integer presentation types are:</p>
  590. <blockquote>
  591. <div><table class="docutils align-default">
  592. <thead>
  593. <tr class="row-odd"><th class="head"><p>Type</p></th>
  594. <th class="head"><p>Meaning</p></th>
  595. </tr>
  596. </thead>
  597. <tbody>
  598. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'b'</span></code></p></td>
  599. <td><p>Binary format. Outputs the number in base 2.</p></td>
  600. </tr>
  601. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'c'</span></code></p></td>
  602. <td><p>Character. Converts the integer to the corresponding
  603. unicode character before printing.</p></td>
  604. </tr>
  605. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'d'</span></code></p></td>
  606. <td><p>Decimal Integer. Outputs the number in base 10.</p></td>
  607. </tr>
  608. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'o'</span></code></p></td>
  609. <td><p>Octal format. Outputs the number in base 8.</p></td>
  610. </tr>
  611. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'x'</span></code></p></td>
  612. <td><p>Hex format. Outputs the number in base 16, using
  613. lower-case letters for the digits above 9.</p></td>
  614. </tr>
  615. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'X'</span></code></p></td>
  616. <td><p>Hex format. Outputs the number in base 16, using
  617. upper-case letters for the digits above 9.
  618. In case <code class="docutils literal notranslate"><span class="pre">'#'</span></code> is specified, the prefix <code class="docutils literal notranslate"><span class="pre">'0x'</span></code> will
  619. be upper-cased to <code class="docutils literal notranslate"><span class="pre">'0X'</span></code> as well.</p></td>
  620. </tr>
  621. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'n'</span></code></p></td>
  622. <td><p>Number. This is the same as <code class="docutils literal notranslate"><span class="pre">'d'</span></code>, except that it uses
  623. the current locale setting to insert the appropriate
  624. number separator characters.</p></td>
  625. </tr>
  626. <tr class="row-odd"><td><p>None</p></td>
  627. <td><p>The same as <code class="docutils literal notranslate"><span class="pre">'d'</span></code>.</p></td>
  628. </tr>
  629. </tbody>
  630. </table>
  631. </div></blockquote>
  632. <p>In addition to the above presentation types, integers can be formatted
  633. with the floating point presentation types listed below (except
  634. <code class="docutils literal notranslate"><span class="pre">'n'</span></code> and <code class="docutils literal notranslate"><span class="pre">None</span></code>). When doing so, <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-func docutils literal notranslate"><span class="pre">float()</span></code></a> is used to convert the
  635. integer to a floating point number before formatting.</p>
  636. <p>The available presentation types for <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> and
  637. <a class="reference internal" href="decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a> values are:</p>
  638. <blockquote>
  639. <div><table class="docutils align-default">
  640. <thead>
  641. <tr class="row-odd"><th class="head"><p>Type</p></th>
  642. <th class="head"><p>Meaning</p></th>
  643. </tr>
  644. </thead>
  645. <tbody>
  646. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'e'</span></code></p></td>
  647. <td><p>Scientific notation. For a given precision <code class="docutils literal notranslate"><span class="pre">p</span></code>,
  648. formats the number in scientific notation with the
  649. letter ‘e’ separating the coefficient from the exponent.
  650. The coefficient has one digit before and <code class="docutils literal notranslate"><span class="pre">p</span></code> digits
  651. after the decimal point, for a total of <code class="docutils literal notranslate"><span class="pre">p</span> <span class="pre">+</span> <span class="pre">1</span></code>
  652. significant digits. With no precision given, uses a
  653. precision of <code class="docutils literal notranslate"><span class="pre">6</span></code> digits after the decimal point for
  654. <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>, and shows all coefficient digits
  655. for <a class="reference internal" href="decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a>. If no digits follow the
  656. decimal point, the decimal point is also removed unless
  657. the <code class="docutils literal notranslate"><span class="pre">#</span></code> option is used.</p></td>
  658. </tr>
  659. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'E'</span></code></p></td>
  660. <td><p>Scientific notation. Same as <code class="docutils literal notranslate"><span class="pre">'e'</span></code> except it uses
  661. an upper case ‘E’ as the separator character.</p></td>
  662. </tr>
  663. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'f'</span></code></p></td>
  664. <td><p>Fixed-point notation. For a given precision <code class="docutils literal notranslate"><span class="pre">p</span></code>,
  665. formats the number as a decimal number with exactly
  666. <code class="docutils literal notranslate"><span class="pre">p</span></code> digits following the decimal point. With no
  667. precision given, uses a precision of <code class="docutils literal notranslate"><span class="pre">6</span></code> digits after
  668. the decimal point for <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>, and uses a
  669. precision large enough to show all coefficient digits
  670. for <a class="reference internal" href="decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a>. If no digits follow the
  671. decimal point, the decimal point is also removed unless
  672. the <code class="docutils literal notranslate"><span class="pre">#</span></code> option is used.</p></td>
  673. </tr>
  674. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'F'</span></code></p></td>
  675. <td><p>Fixed-point notation. Same as <code class="docutils literal notranslate"><span class="pre">'f'</span></code>, but converts
  676. <code class="docutils literal notranslate"><span class="pre">nan</span></code> to <code class="docutils literal notranslate"><span class="pre">NAN</span></code> and <code class="docutils literal notranslate"><span class="pre">inf</span></code> to <code class="docutils literal notranslate"><span class="pre">INF</span></code>.</p></td>
  677. </tr>
  678. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'g'</span></code></p></td>
  679. <td><p>General format. For a given precision <code class="docutils literal notranslate"><span class="pre">p</span> <span class="pre">&gt;=</span> <span class="pre">1</span></code>,
  680. this rounds the number to <code class="docutils literal notranslate"><span class="pre">p</span></code> significant digits and
  681. then formats the result in either fixed-point format
  682. or in scientific notation, depending on its magnitude.
  683. A precision of <code class="docutils literal notranslate"><span class="pre">0</span></code> is treated as equivalent to a
  684. precision of <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  685. <p>The precise rules are as follows: suppose that the
  686. result formatted with presentation type <code class="docutils literal notranslate"><span class="pre">'e'</span></code> and
  687. precision <code class="docutils literal notranslate"><span class="pre">p-1</span></code> would have exponent <code class="docutils literal notranslate"><span class="pre">exp</span></code>. Then,
  688. if <code class="docutils literal notranslate"><span class="pre">m</span> <span class="pre">&lt;=</span> <span class="pre">exp</span> <span class="pre">&lt;</span> <span class="pre">p</span></code>, where <code class="docutils literal notranslate"><span class="pre">m</span></code> is -4 for floats and -6
  689. for <a class="reference internal" href="decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimals</span></code></a>, the number is
  690. formatted with presentation type <code class="docutils literal notranslate"><span class="pre">'f'</span></code> and precision
  691. <code class="docutils literal notranslate"><span class="pre">p-1-exp</span></code>. Otherwise, the number is formatted
  692. with presentation type <code class="docutils literal notranslate"><span class="pre">'e'</span></code> and precision <code class="docutils literal notranslate"><span class="pre">p-1</span></code>.
  693. In both cases insignificant trailing zeros are removed
  694. from the significand, and the decimal point is also
  695. removed if there are no remaining digits following it,
  696. unless the <code class="docutils literal notranslate"><span class="pre">'#'</span></code> option is used.</p>
  697. <p>With no precision given, uses a precision of <code class="docutils literal notranslate"><span class="pre">6</span></code>
  698. significant digits for <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>. For
  699. <a class="reference internal" href="decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a>, the coefficient of the result
  700. is formed from the coefficient digits of the value;
  701. scientific notation is used for values smaller than
  702. <code class="docutils literal notranslate"><span class="pre">1e-6</span></code> in absolute value and values where the place
  703. value of the least significant digit is larger than 1,
  704. and fixed-point notation is used otherwise.</p>
  705. <p>Positive and negative infinity, positive and negative
  706. zero, and nans, are formatted as <code class="docutils literal notranslate"><span class="pre">inf</span></code>, <code class="docutils literal notranslate"><span class="pre">-inf</span></code>,
  707. <code class="docutils literal notranslate"><span class="pre">0</span></code>, <code class="docutils literal notranslate"><span class="pre">-0</span></code> and <code class="docutils literal notranslate"><span class="pre">nan</span></code> respectively, regardless of
  708. the precision.</p>
  709. </td>
  710. </tr>
  711. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'G'</span></code></p></td>
  712. <td><p>General format. Same as <code class="docutils literal notranslate"><span class="pre">'g'</span></code> except switches to
  713. <code class="docutils literal notranslate"><span class="pre">'E'</span></code> if the number gets too large. The
  714. representations of infinity and NaN are uppercased, too.</p></td>
  715. </tr>
  716. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'n'</span></code></p></td>
  717. <td><p>Number. This is the same as <code class="docutils literal notranslate"><span class="pre">'g'</span></code>, except that it uses
  718. the current locale setting to insert the appropriate
  719. number separator characters.</p></td>
  720. </tr>
  721. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'%'</span></code></p></td>
  722. <td><p>Percentage. Multiplies the number by 100 and displays
  723. in fixed (<code class="docutils literal notranslate"><span class="pre">'f'</span></code>) format, followed by a percent sign.</p></td>
  724. </tr>
  725. <tr class="row-even"><td><p>None</p></td>
  726. <td><p>For <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> this is the same as <code class="docutils literal notranslate"><span class="pre">'g'</span></code>, except
  727. that when fixed-point notation is used to format the
  728. result, it always includes at least one digit past the
  729. decimal point. The precision used is as large as needed
  730. to represent the given value faithfully.</p>
  731. <p>For <a class="reference internal" href="decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Decimal</span></code></a>, this is the same as
  732. either <code class="docutils literal notranslate"><span class="pre">'g'</span></code> or <code class="docutils literal notranslate"><span class="pre">'G'</span></code> depending on the value of
  733. <code class="docutils literal notranslate"><span class="pre">context.capitals</span></code> for the current decimal context.</p>
  734. <p>The overall effect is to match the output of <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a>
  735. as altered by the other format modifiers.</p>
  736. </td>
  737. </tr>
  738. </tbody>
  739. </table>
  740. </div></blockquote>
  741. </section>
  742. <section id="format-examples">
  743. <span id="formatexamples"></span><h3>Format examples<a class="headerlink" href="#format-examples" title="Link to this heading">¶</a></h3>
  744. <p>This section contains examples of the <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a> syntax and
  745. comparison with the old <code class="docutils literal notranslate"><span class="pre">%</span></code>-formatting.</p>
  746. <p>In most of the cases the syntax is similar to the old <code class="docutils literal notranslate"><span class="pre">%</span></code>-formatting, with the
  747. addition of the <code class="docutils literal notranslate"><span class="pre">{}</span></code> and with <code class="docutils literal notranslate"><span class="pre">:</span></code> used instead of <code class="docutils literal notranslate"><span class="pre">%</span></code>.
  748. For example, <code class="docutils literal notranslate"><span class="pre">'%03.2f'</span></code> can be translated to <code class="docutils literal notranslate"><span class="pre">'{:03.2f}'</span></code>.</p>
  749. <p>The new format syntax also supports new and different options, shown in the
  750. following examples.</p>
  751. <p>Accessing arguments by position:</p>
  752. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{0}</span><span class="s1">, </span><span class="si">{1}</span><span class="s1">, </span><span class="si">{2}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">)</span>
  753. <span class="go">&#39;a, b, c&#39;</span>
  754. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">, </span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">)</span> <span class="c1"># 3.1+ only</span>
  755. <span class="go">&#39;a, b, c&#39;</span>
  756. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{2}</span><span class="s1">, </span><span class="si">{1}</span><span class="s1">, </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="s1">&#39;b&#39;</span><span class="p">,</span> <span class="s1">&#39;c&#39;</span><span class="p">)</span>
  757. <span class="go">&#39;c, b, a&#39;</span>
  758. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{2}</span><span class="s1">, </span><span class="si">{1}</span><span class="s1">, </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">*</span><span class="s1">&#39;abc&#39;</span><span class="p">)</span> <span class="c1"># unpacking argument sequence</span>
  759. <span class="go">&#39;c, b, a&#39;</span>
  760. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{0}{1}{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;abra&#39;</span><span class="p">,</span> <span class="s1">&#39;cad&#39;</span><span class="p">)</span> <span class="c1"># arguments&#39; indices can be repeated</span>
  761. <span class="go">&#39;abracadabra&#39;</span>
  762. </pre></div>
  763. </div>
  764. <p>Accessing arguments by name:</p>
  765. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;Coordinates: </span><span class="si">{latitude}</span><span class="s1">, </span><span class="si">{longitude}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">latitude</span><span class="o">=</span><span class="s1">&#39;37.24N&#39;</span><span class="p">,</span> <span class="n">longitude</span><span class="o">=</span><span class="s1">&#39;-115.81W&#39;</span><span class="p">)</span>
  766. <span class="go">&#39;Coordinates: 37.24N, -115.81W&#39;</span>
  767. <span class="gp">&gt;&gt;&gt; </span><span class="n">coord</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;latitude&#39;</span><span class="p">:</span> <span class="s1">&#39;37.24N&#39;</span><span class="p">,</span> <span class="s1">&#39;longitude&#39;</span><span class="p">:</span> <span class="s1">&#39;-115.81W&#39;</span><span class="p">}</span>
  768. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;Coordinates: </span><span class="si">{latitude}</span><span class="s1">, </span><span class="si">{longitude}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">**</span><span class="n">coord</span><span class="p">)</span>
  769. <span class="go">&#39;Coordinates: 37.24N, -115.81W&#39;</span>
  770. </pre></div>
  771. </div>
  772. <p>Accessing arguments’ attributes:</p>
  773. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="mi">3</span><span class="o">-</span><span class="mi">5</span><span class="n">j</span>
  774. <span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="s1">&#39;The complex number </span><span class="si">{0}</span><span class="s1"> is formed from the real part </span><span class="si">{0.real}</span><span class="s1"> &#39;</span>
  775. <span class="gp">... </span> <span class="s1">&#39;and the imaginary part </span><span class="si">{0.imag}</span><span class="s1">.&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">c</span><span class="p">)</span>
  776. <span class="go">&#39;The complex number (3-5j) is formed from the real part 3.0 and the imaginary part -5.0.&#39;</span>
  777. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Point</span><span class="p">:</span>
  778. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
  779. <span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
  780. <span class="gp">... </span> <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
  781. <span class="gp">... </span> <span class="k">return</span> <span class="s1">&#39;Point(</span><span class="si">{self.x}</span><span class="s1">, </span><span class="si">{self.y}</span><span class="s1">)&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
  782. <span class="gp">...</span>
  783. <span class="gp">&gt;&gt;&gt; </span><span class="nb">str</span><span class="p">(</span><span class="n">Point</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
  784. <span class="go">&#39;Point(4, 2)&#39;</span>
  785. </pre></div>
  786. </div>
  787. <p>Accessing arguments’ items:</p>
  788. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">coord</span> <span class="o">=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
  789. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;X: </span><span class="si">{0[0]}</span><span class="s1">; Y: </span><span class="si">{0[1]}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">coord</span><span class="p">)</span>
  790. <span class="go">&#39;X: 3; Y: 5&#39;</span>
  791. </pre></div>
  792. </div>
  793. <p>Replacing <code class="docutils literal notranslate"><span class="pre">%s</span></code> and <code class="docutils literal notranslate"><span class="pre">%r</span></code>:</p>
  794. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s2">&quot;repr() shows quotes: </span><span class="si">{!r}</span><span class="s2">; str() doesn&#39;t: </span><span class="si">{!s}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;test1&#39;</span><span class="p">,</span> <span class="s1">&#39;test2&#39;</span><span class="p">)</span>
  795. <span class="go">&quot;repr() shows quotes: &#39;test1&#39;; str() doesn&#39;t: test2&quot;</span>
  796. </pre></div>
  797. </div>
  798. <p>Aligning the text and specifying a width:</p>
  799. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:&lt;30}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;left aligned&#39;</span><span class="p">)</span>
  800. <span class="go">&#39;left aligned &#39;</span>
  801. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:&gt;30}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;right aligned&#39;</span><span class="p">)</span>
  802. <span class="go">&#39; right aligned&#39;</span>
  803. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:^30}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;centered&#39;</span><span class="p">)</span>
  804. <span class="go">&#39; centered &#39;</span>
  805. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:*^30}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s1">&#39;centered&#39;</span><span class="p">)</span> <span class="c1"># use &#39;*&#39; as a fill char</span>
  806. <span class="go">&#39;***********centered***********&#39;</span>
  807. </pre></div>
  808. </div>
  809. <p>Replacing <code class="docutils literal notranslate"><span class="pre">%+f</span></code>, <code class="docutils literal notranslate"><span class="pre">%-f</span></code>, and <code class="docutils literal notranslate"><span class="pre">%</span> <span class="pre">f</span></code> and specifying a sign:</p>
  810. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:+f}</span><span class="s1">; </span><span class="si">{:+f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">3.14</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.14</span><span class="p">)</span> <span class="c1"># show it always</span>
  811. <span class="go">&#39;+3.140000; -3.140000&#39;</span>
  812. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{: f}</span><span class="s1">; </span><span class="si">{: f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">3.14</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.14</span><span class="p">)</span> <span class="c1"># show a space for positive numbers</span>
  813. <span class="go">&#39; 3.140000; -3.140000&#39;</span>
  814. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:-f}</span><span class="s1">; </span><span class="si">{:-f}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">3.14</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.14</span><span class="p">)</span> <span class="c1"># show only the minus -- same as &#39;{:f}; {:f}&#39;</span>
  815. <span class="go">&#39;3.140000; -3.140000&#39;</span>
  816. </pre></div>
  817. </div>
  818. <p>Replacing <code class="docutils literal notranslate"><span class="pre">%x</span></code> and <code class="docutils literal notranslate"><span class="pre">%o</span></code> and converting the value to different bases:</p>
  819. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># format also supports binary numbers</span>
  820. <span class="gp">&gt;&gt;&gt; </span><span class="s2">&quot;int: </span><span class="si">{0:d}</span><span class="s2">; hex: </span><span class="si">{0:x}</span><span class="s2">; oct: </span><span class="si">{0:o}</span><span class="s2">; bin: </span><span class="si">{0:b}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
  821. <span class="go">&#39;int: 42; hex: 2a; oct: 52; bin: 101010&#39;</span>
  822. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># with 0x, 0o, or 0b as prefix:</span>
  823. <span class="gp">&gt;&gt;&gt; </span><span class="s2">&quot;int: </span><span class="si">{0:d}</span><span class="s2">; hex: </span><span class="si">{0:#x}</span><span class="s2">; oct: </span><span class="si">{0:#o}</span><span class="s2">; bin: </span><span class="si">{0:#b}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
  824. <span class="go">&#39;int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010&#39;</span>
  825. </pre></div>
  826. </div>
  827. <p>Using the comma as a thousands separator:</p>
  828. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:,}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mi">1234567890</span><span class="p">)</span>
  829. <span class="go">&#39;1,234,567,890&#39;</span>
  830. </pre></div>
  831. </div>
  832. <p>Expressing a percentage:</p>
  833. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">points</span> <span class="o">=</span> <span class="mi">19</span>
  834. <span class="gp">&gt;&gt;&gt; </span><span class="n">total</span> <span class="o">=</span> <span class="mi">22</span>
  835. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;Correct answers: </span><span class="si">{:.2%}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">points</span><span class="o">/</span><span class="n">total</span><span class="p">)</span>
  836. <span class="go">&#39;Correct answers: 86.36%&#39;</span>
  837. </pre></div>
  838. </div>
  839. <p>Using type-specific formatting:</p>
  840. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">datetime</span>
  841. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">(</span><span class="mi">2010</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">58</span><span class="p">)</span>
  842. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;{:%Y-%m-</span><span class="si">%d</span><span class="s1"> %H:%M:%S}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  843. <span class="go">&#39;2010-07-04 12:15:58&#39;</span>
  844. </pre></div>
  845. </div>
  846. <p>Nesting arguments and more complex examples:</p>
  847. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">align</span><span class="p">,</span> <span class="n">text</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="s1">&#39;&lt;^&gt;&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;left&#39;</span><span class="p">,</span> <span class="s1">&#39;center&#39;</span><span class="p">,</span> <span class="s1">&#39;right&#39;</span><span class="p">]):</span>
  848. <span class="gp">... </span> <span class="s1">&#39;{0:</span><span class="si">{fill}{align}</span><span class="s1">16}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">fill</span><span class="o">=</span><span class="n">align</span><span class="p">,</span> <span class="n">align</span><span class="o">=</span><span class="n">align</span><span class="p">)</span>
  849. <span class="gp">...</span>
  850. <span class="go">&#39;left&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&#39;</span>
  851. <span class="go">&#39;^^^^^center^^^^^&#39;</span>
  852. <span class="go">&#39;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;right&#39;</span>
  853. <span class="gp">&gt;&gt;&gt;</span>
  854. <span class="gp">&gt;&gt;&gt; </span><span class="n">octets</span> <span class="o">=</span> <span class="p">[</span><span class="mi">192</span><span class="p">,</span> <span class="mi">168</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
  855. <span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;</span><span class="si">{:02X}{:02X}{:02X}{:02X}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">*</span><span class="n">octets</span><span class="p">)</span>
  856. <span class="go">&#39;C0A80001&#39;</span>
  857. <span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span>
  858. <span class="go">3232235521</span>
  859. <span class="gp">&gt;&gt;&gt;</span>
  860. <span class="gp">&gt;&gt;&gt; </span><span class="n">width</span> <span class="o">=</span> <span class="mi">5</span>
  861. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">12</span><span class="p">):</span>
  862. <span class="gp">... </span> <span class="k">for</span> <span class="n">base</span> <span class="ow">in</span> <span class="s1">&#39;dXob&#39;</span><span class="p">:</span>
  863. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;{0:</span><span class="si">{width}{base}</span><span class="s1">}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">base</span><span class="o">=</span><span class="n">base</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="n">width</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
  864. <span class="gp">... </span> <span class="nb">print</span><span class="p">()</span>
  865. <span class="gp">...</span>
  866. <span class="go"> 5 5 5 101</span>
  867. <span class="go"> 6 6 6 110</span>
  868. <span class="go"> 7 7 7 111</span>
  869. <span class="go"> 8 8 10 1000</span>
  870. <span class="go"> 9 9 11 1001</span>
  871. <span class="go"> 10 A 12 1010</span>
  872. <span class="go"> 11 B 13 1011</span>
  873. </pre></div>
  874. </div>
  875. </section>
  876. </section>
  877. <section id="template-strings">
  878. <span id="id1"></span><h2>Template strings<a class="headerlink" href="#template-strings" title="Link to this heading">¶</a></h2>
  879. <p>Template strings provide simpler string substitutions as described in
  880. <span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-0292/"><strong>PEP 292</strong></a>. A primary use case for template strings is for
  881. internationalization (i18n) since in that context, the simpler syntax and
  882. functionality makes it easier to translate than other built-in string
  883. formatting facilities in Python. As an example of a library built on template
  884. strings for i18n, see the
  885. <a class="reference external" href="https://flufli18n.readthedocs.io/en/latest/">flufl.i18n</a> package.</p>
  886. <p id="index-13">Template strings support <code class="docutils literal notranslate"><span class="pre">$</span></code>-based substitutions, using the following rules:</p>
  887. <ul class="simple">
  888. <li><p><code class="docutils literal notranslate"><span class="pre">$$</span></code> is an escape; it is replaced with a single <code class="docutils literal notranslate"><span class="pre">$</span></code>.</p></li>
  889. <li><p><code class="docutils literal notranslate"><span class="pre">$identifier</span></code> names a substitution placeholder matching a mapping key of
  890. <code class="docutils literal notranslate"><span class="pre">&quot;identifier&quot;</span></code>. By default, <code class="docutils literal notranslate"><span class="pre">&quot;identifier&quot;</span></code> is restricted to any
  891. case-insensitive ASCII alphanumeric string (including underscores) that
  892. starts with an underscore or ASCII letter. The first non-identifier
  893. character after the <code class="docutils literal notranslate"><span class="pre">$</span></code> character terminates this placeholder
  894. specification.</p></li>
  895. <li><p><code class="docutils literal notranslate"><span class="pre">${identifier}</span></code> is equivalent to <code class="docutils literal notranslate"><span class="pre">$identifier</span></code>. It is required when
  896. valid identifier characters follow the placeholder but are not part of the
  897. placeholder, such as <code class="docutils literal notranslate"><span class="pre">&quot;${noun}ification&quot;</span></code>.</p></li>
  898. </ul>
  899. <p>Any other appearance of <code class="docutils literal notranslate"><span class="pre">$</span></code> in the string will result in a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>
  900. being raised.</p>
  901. <p>The <a class="reference internal" href="#module-string" title="string: Common string operations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code></a> module provides a <a class="reference internal" href="#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> class that implements
  902. these rules. The methods of <a class="reference internal" href="#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> are:</p>
  903. <dl class="py class">
  904. <dt class="sig sig-object py" id="string.Template">
  905. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">Template</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">template</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Template" title="Link to this definition">¶</a></dt>
  906. <dd><p>The constructor takes a single argument which is the template string.</p>
  907. <dl class="py method">
  908. <dt class="sig sig-object py" id="string.Template.substitute">
  909. <span class="sig-name descname"><span class="pre">substitute</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mapping</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Template.substitute" title="Link to this definition">¶</a></dt>
  910. <dd><p>Performs the template substitution, returning a new string. <em>mapping</em> is
  911. any dictionary-like object with keys that match the placeholders in the
  912. template. Alternatively, you can provide keyword arguments, where the
  913. keywords are the placeholders. When both <em>mapping</em> and <em>kwds</em> are given
  914. and there are duplicates, the placeholders from <em>kwds</em> take precedence.</p>
  915. </dd></dl>
  916. <dl class="py method">
  917. <dt class="sig sig-object py" id="string.Template.safe_substitute">
  918. <span class="sig-name descname"><span class="pre">safe_substitute</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">mapping</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.Template.safe_substitute" title="Link to this definition">¶</a></dt>
  919. <dd><p>Like <a class="reference internal" href="#string.Template.substitute" title="string.Template.substitute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">substitute()</span></code></a>, except that if placeholders are missing from
  920. <em>mapping</em> and <em>kwds</em>, instead of raising a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception, the
  921. original placeholder will appear in the resulting string intact. Also,
  922. unlike with <a class="reference internal" href="#string.Template.substitute" title="string.Template.substitute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">substitute()</span></code></a>, any other appearances of the <code class="docutils literal notranslate"><span class="pre">$</span></code> will
  923. simply return <code class="docutils literal notranslate"><span class="pre">$</span></code> instead of raising <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  924. <p>While other exceptions may still occur, this method is called “safe”
  925. because it always tries to return a usable string instead of
  926. raising an exception. In another sense, <a class="reference internal" href="#string.Template.safe_substitute" title="string.Template.safe_substitute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">safe_substitute()</span></code></a> may be
  927. anything other than safe, since it will silently ignore malformed
  928. templates containing dangling delimiters, unmatched braces, or
  929. placeholders that are not valid Python identifiers.</p>
  930. </dd></dl>
  931. <dl class="py method">
  932. <dt class="sig sig-object py" id="string.Template.is_valid">
  933. <span class="sig-name descname"><span class="pre">is_valid</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#string.Template.is_valid" title="Link to this definition">¶</a></dt>
  934. <dd><p>Returns false if the template has invalid placeholders that will cause
  935. <a class="reference internal" href="#string.Template.substitute" title="string.Template.substitute"><code class="xref py py-meth docutils literal notranslate"><span class="pre">substitute()</span></code></a> to raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
  936. <div class="versionadded">
  937. <p><span class="versionmodified added">New in version 3.11.</span></p>
  938. </div>
  939. </dd></dl>
  940. <dl class="py method">
  941. <dt class="sig sig-object py" id="string.Template.get_identifiers">
  942. <span class="sig-name descname"><span class="pre">get_identifiers</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#string.Template.get_identifiers" title="Link to this definition">¶</a></dt>
  943. <dd><p>Returns a list of the valid identifiers in the template, in the order
  944. they first appear, ignoring any invalid identifiers.</p>
  945. <div class="versionadded">
  946. <p><span class="versionmodified added">New in version 3.11.</span></p>
  947. </div>
  948. </dd></dl>
  949. <p><a class="reference internal" href="#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> instances also provide one public data attribute:</p>
  950. <dl class="py attribute">
  951. <dt class="sig sig-object py" id="string.Template.template">
  952. <span class="sig-name descname"><span class="pre">template</span></span><a class="headerlink" href="#string.Template.template" title="Link to this definition">¶</a></dt>
  953. <dd><p>This is the object passed to the constructor’s <em>template</em> argument. In
  954. general, you shouldn’t change it, but read-only access is not enforced.</p>
  955. </dd></dl>
  956. </dd></dl>
  957. <p>Here is an example of how to use a Template:</p>
  958. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">string</span> <span class="kn">import</span> <span class="n">Template</span>
  959. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s1">&#39;$who likes $what&#39;</span><span class="p">)</span>
  960. <span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">substitute</span><span class="p">(</span><span class="n">who</span><span class="o">=</span><span class="s1">&#39;tim&#39;</span><span class="p">,</span> <span class="n">what</span><span class="o">=</span><span class="s1">&#39;kung pao&#39;</span><span class="p">)</span>
  961. <span class="go">&#39;tim likes kung pao&#39;</span>
  962. <span class="gp">&gt;&gt;&gt; </span><span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">who</span><span class="o">=</span><span class="s1">&#39;tim&#39;</span><span class="p">)</span>
  963. <span class="gp">&gt;&gt;&gt; </span><span class="n">Template</span><span class="p">(</span><span class="s1">&#39;Give $who $100&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">substitute</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  964. <span class="gt">Traceback (most recent call last):</span>
  965. <span class="c">...</span>
  966. <span class="gr">ValueError</span>: <span class="n">Invalid placeholder in string: line 1, col 11</span>
  967. <span class="gp">&gt;&gt;&gt; </span><span class="n">Template</span><span class="p">(</span><span class="s1">&#39;$who likes $what&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">substitute</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  968. <span class="gt">Traceback (most recent call last):</span>
  969. <span class="c">...</span>
  970. <span class="gr">KeyError</span>: <span class="n">&#39;what&#39;</span>
  971. <span class="gp">&gt;&gt;&gt; </span><span class="n">Template</span><span class="p">(</span><span class="s1">&#39;$who likes $what&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">safe_substitute</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
  972. <span class="go">&#39;tim likes $what&#39;</span>
  973. </pre></div>
  974. </div>
  975. <p>Advanced usage: you can derive subclasses of <a class="reference internal" href="#string.Template" title="string.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a> to customize
  976. the placeholder syntax, delimiter character, or the entire regular expression
  977. used to parse template strings. To do this, you can override these class
  978. attributes:</p>
  979. <ul>
  980. <li><p><em>delimiter</em> – This is the literal string describing a placeholder
  981. introducing delimiter. The default value is <code class="docutils literal notranslate"><span class="pre">$</span></code>. Note that this should
  982. <em>not</em> be a regular expression, as the implementation will call
  983. <a class="reference internal" href="re.html#re.escape" title="re.escape"><code class="xref py py-meth docutils literal notranslate"><span class="pre">re.escape()</span></code></a> on this string as needed. Note further that you cannot
  984. change the delimiter after class creation (i.e. a different delimiter must
  985. be set in the subclass’s class namespace).</p></li>
  986. <li><p><em>idpattern</em> – This is the regular expression describing the pattern for
  987. non-braced placeholders. The default value is the regular expression
  988. <code class="docutils literal notranslate"><span class="pre">(?a:[_a-z][_a-z0-9]*)</span></code>. If this is given and <em>braceidpattern</em> is
  989. <code class="docutils literal notranslate"><span class="pre">None</span></code> this pattern will also apply to braced placeholders.</p>
  990. <div class="admonition note">
  991. <p class="admonition-title">Note</p>
  992. <p>Since default <em>flags</em> is <code class="docutils literal notranslate"><span class="pre">re.IGNORECASE</span></code>, pattern <code class="docutils literal notranslate"><span class="pre">[a-z]</span></code> can match
  993. with some non-ASCII characters. That’s why we use the local <code class="docutils literal notranslate"><span class="pre">a</span></code> flag
  994. here.</p>
  995. </div>
  996. <div class="versionchanged">
  997. <p><span class="versionmodified changed">Changed in version 3.7: </span><em>braceidpattern</em> can be used to define separate patterns used inside and
  998. outside the braces.</p>
  999. </div>
  1000. </li>
  1001. <li><p><em>braceidpattern</em> – This is like <em>idpattern</em> but describes the pattern for
  1002. braced placeholders. Defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code> which means to fall back to
  1003. <em>idpattern</em> (i.e. the same pattern is used both inside and outside braces).
  1004. If given, this allows you to define different patterns for braced and
  1005. unbraced placeholders.</p>
  1006. <div class="versionadded">
  1007. <p><span class="versionmodified added">New in version 3.7.</span></p>
  1008. </div>
  1009. </li>
  1010. <li><p><em>flags</em> – The regular expression flags that will be applied when compiling
  1011. the regular expression used for recognizing substitutions. The default value
  1012. is <code class="docutils literal notranslate"><span class="pre">re.IGNORECASE</span></code>. Note that <code class="docutils literal notranslate"><span class="pre">re.VERBOSE</span></code> will always be added to the
  1013. flags, so custom <em>idpattern</em>s must follow conventions for verbose regular
  1014. expressions.</p>
  1015. <div class="versionadded">
  1016. <p><span class="versionmodified added">New in version 3.2.</span></p>
  1017. </div>
  1018. </li>
  1019. </ul>
  1020. <p>Alternatively, you can provide the entire regular expression pattern by
  1021. overriding the class attribute <em>pattern</em>. If you do this, the value must be a
  1022. regular expression object with four named capturing groups. The capturing
  1023. groups correspond to the rules given above, along with the invalid placeholder
  1024. rule:</p>
  1025. <ul class="simple">
  1026. <li><p><em>escaped</em> – This group matches the escape sequence, e.g. <code class="docutils literal notranslate"><span class="pre">$$</span></code>, in the
  1027. default pattern.</p></li>
  1028. <li><p><em>named</em> – This group matches the unbraced placeholder name; it should not
  1029. include the delimiter in capturing group.</p></li>
  1030. <li><p><em>braced</em> – This group matches the brace enclosed placeholder name; it should
  1031. not include either the delimiter or braces in the capturing group.</p></li>
  1032. <li><p><em>invalid</em> – This group matches any other delimiter pattern (usually a single
  1033. delimiter), and it should appear last in the regular expression.</p></li>
  1034. </ul>
  1035. <p>The methods on this class will raise <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if the pattern matches
  1036. the template without one of these named groups matching.</p>
  1037. </section>
  1038. <section id="helper-functions">
  1039. <h2>Helper functions<a class="headerlink" href="#helper-functions" title="Link to this heading">¶</a></h2>
  1040. <dl class="py function">
  1041. <dt class="sig sig-object py" id="string.capwords">
  1042. <span class="sig-prename descclassname"><span class="pre">string.</span></span><span class="sig-name descname"><span class="pre">capwords</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sep</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#string.capwords" title="Link to this definition">¶</a></dt>
  1043. <dd><p>Split the argument into words using <a class="reference internal" href="stdtypes.html#str.split" title="str.split"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.split()</span></code></a>, capitalize each word
  1044. using <a class="reference internal" href="stdtypes.html#str.capitalize" title="str.capitalize"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.capitalize()</span></code></a>, and join the capitalized words using
  1045. <a class="reference internal" href="stdtypes.html#str.join" title="str.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.join()</span></code></a>. If the optional second argument <em>sep</em> is absent
  1046. or <code class="docutils literal notranslate"><span class="pre">None</span></code>, runs of whitespace characters are replaced by a single space
  1047. and leading and trailing whitespace are removed, otherwise <em>sep</em> is used to
  1048. split and join the words.</p>
  1049. </dd></dl>
  1050. </section>
  1051. </section>
  1052. <div class="clearer"></div>
  1053. </div>
  1054. </div>
  1055. </div>
  1056. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1057. <div class="sphinxsidebarwrapper">
  1058. <div>
  1059. <h3><a href="../contents.html">Table of Contents</a></h3>
  1060. <ul>
  1061. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code> — Common string operations</a><ul>
  1062. <li><a class="reference internal" href="#string-constants">String constants</a></li>
  1063. <li><a class="reference internal" href="#custom-string-formatting">Custom String Formatting</a></li>
  1064. <li><a class="reference internal" href="#format-string-syntax">Format String Syntax</a><ul>
  1065. <li><a class="reference internal" href="#format-specification-mini-language">Format Specification Mini-Language</a></li>
  1066. <li><a class="reference internal" href="#format-examples">Format examples</a></li>
  1067. </ul>
  1068. </li>
  1069. <li><a class="reference internal" href="#template-strings">Template strings</a></li>
  1070. <li><a class="reference internal" href="#helper-functions">Helper functions</a></li>
  1071. </ul>
  1072. </li>
  1073. </ul>
  1074. </div>
  1075. <div>
  1076. <h4>Previous topic</h4>
  1077. <p class="topless"><a href="text.html"
  1078. title="previous chapter">Text Processing Services</a></p>
  1079. </div>
  1080. <div>
  1081. <h4>Next topic</h4>
  1082. <p class="topless"><a href="re.html"
  1083. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">re</span></code> — Regular expression operations</a></p>
  1084. </div>
  1085. <div role="note" aria-label="source link">
  1086. <h3>This Page</h3>
  1087. <ul class="this-page-menu">
  1088. <li><a href="../bugs.html">Report a Bug</a></li>
  1089. <li>
  1090. <a href="https://github.com/python/cpython/blob/main/Doc/library/string.rst"
  1091. rel="nofollow">Show Source
  1092. </a>
  1093. </li>
  1094. </ul>
  1095. </div>
  1096. </div>
  1097. <div id="sidebarbutton" title="Collapse sidebar">
  1098. <span>«</span>
  1099. </div>
  1100. </div>
  1101. <div class="clearer"></div>
  1102. </div>
  1103. <div class="related" role="navigation" aria-label="related navigation">
  1104. <h3>Navigation</h3>
  1105. <ul>
  1106. <li class="right" style="margin-right: 10px">
  1107. <a href="../genindex.html" title="General Index"
  1108. >index</a></li>
  1109. <li class="right" >
  1110. <a href="../py-modindex.html" title="Python Module Index"
  1111. >modules</a> |</li>
  1112. <li class="right" >
  1113. <a href="re.html" title="re — Regular expression operations"
  1114. >next</a> |</li>
  1115. <li class="right" >
  1116. <a href="text.html" title="Text Processing Services"
  1117. >previous</a> |</li>
  1118. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1119. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1120. <li class="switchers">
  1121. <div class="language_switcher_placeholder"></div>
  1122. <div class="version_switcher_placeholder"></div>
  1123. </li>
  1124. <li>
  1125. </li>
  1126. <li id="cpython-language-and-version">
  1127. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1128. </li>
  1129. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1130. <li class="nav-item nav-item-2"><a href="text.html" >Text Processing Services</a> &#187;</li>
  1131. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">string</span></code> — Common string operations</a></li>
  1132. <li class="right">
  1133. <div class="inline-search" role="search">
  1134. <form class="inline-search" action="../search.html" method="get">
  1135. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1136. <input type="submit" value="Go" />
  1137. </form>
  1138. </div>
  1139. |
  1140. </li>
  1141. <li class="right">
  1142. <label class="theme-selector-label">
  1143. Theme
  1144. <select class="theme-selector" oninput="activateTheme(this.value)">
  1145. <option value="auto" selected>Auto</option>
  1146. <option value="light">Light</option>
  1147. <option value="dark">Dark</option>
  1148. </select>
  1149. </label> |</li>
  1150. </ul>
  1151. </div>
  1152. <div class="footer">
  1153. &copy;
  1154. <a href="../copyright.html">
  1155. Copyright
  1156. </a>
  1157. 2001-2024, Python Software Foundation.
  1158. <br />
  1159. This page is licensed under the Python Software Foundation License Version 2.
  1160. <br />
  1161. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1162. <br />
  1163. See <a href="/license.html">History and License</a> for more information.<br />
  1164. <br />
  1165. The Python Software Foundation is a non-profit corporation.
  1166. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1167. <br />
  1168. <br />
  1169. Last updated on Apr 09, 2024 (13:47 UTC).
  1170. <a href="/bugs.html">Found a bug</a>?
  1171. <br />
  1172. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1173. </div>
  1174. </body>
  1175. </html>
上海开阖软件有限公司 沪ICP备12045867号-1