gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1202 rindas
103KB

  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="2. Lexical analysis" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/reference/lexical_analysis.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="A Python program is read by a parser. Input to the parser is a stream of tokens, generated by the lexical analyzer. This chapter describes how the lexical analyzer breaks a file into tokens. Python..." />
  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="A Python program is read by a parser. Input to the parser is a stream of tokens, generated by the lexical analyzer. This chapter describes how the lexical analyzer breaks a file into tokens. Python..." />
  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>2. Lexical analysis &#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="3. Data model" href="datamodel.html" />
  33. <link rel="prev" title="1. Introduction" href="introduction.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/reference/lexical_analysis.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="#">2. Lexical analysis</a><ul>
  86. <li><a class="reference internal" href="#line-structure">2.1. Line structure</a><ul>
  87. <li><a class="reference internal" href="#logical-lines">2.1.1. Logical lines</a></li>
  88. <li><a class="reference internal" href="#physical-lines">2.1.2. Physical lines</a></li>
  89. <li><a class="reference internal" href="#comments">2.1.3. Comments</a></li>
  90. <li><a class="reference internal" href="#encoding-declarations">2.1.4. Encoding declarations</a></li>
  91. <li><a class="reference internal" href="#explicit-line-joining">2.1.5. Explicit line joining</a></li>
  92. <li><a class="reference internal" href="#implicit-line-joining">2.1.6. Implicit line joining</a></li>
  93. <li><a class="reference internal" href="#blank-lines">2.1.7. Blank lines</a></li>
  94. <li><a class="reference internal" href="#indentation">2.1.8. Indentation</a></li>
  95. <li><a class="reference internal" href="#whitespace-between-tokens">2.1.9. Whitespace between tokens</a></li>
  96. </ul>
  97. </li>
  98. <li><a class="reference internal" href="#other-tokens">2.2. Other tokens</a></li>
  99. <li><a class="reference internal" href="#identifiers">2.3. Identifiers and keywords</a><ul>
  100. <li><a class="reference internal" href="#keywords">2.3.1. Keywords</a></li>
  101. <li><a class="reference internal" href="#soft-keywords">2.3.2. Soft Keywords</a></li>
  102. <li><a class="reference internal" href="#reserved-classes-of-identifiers">2.3.3. Reserved classes of identifiers</a></li>
  103. </ul>
  104. </li>
  105. <li><a class="reference internal" href="#literals">2.4. Literals</a><ul>
  106. <li><a class="reference internal" href="#string-and-bytes-literals">2.4.1. String and Bytes literals</a><ul>
  107. <li><a class="reference internal" href="#escape-sequences">2.4.1.1. Escape sequences</a></li>
  108. </ul>
  109. </li>
  110. <li><a class="reference internal" href="#string-literal-concatenation">2.4.2. String literal concatenation</a></li>
  111. <li><a class="reference internal" href="#formatted-string-literals">2.4.3. f-strings</a></li>
  112. <li><a class="reference internal" href="#numeric-literals">2.4.4. Numeric literals</a></li>
  113. <li><a class="reference internal" href="#integer-literals">2.4.5. Integer literals</a></li>
  114. <li><a class="reference internal" href="#floating-point-literals">2.4.6. Floating point literals</a></li>
  115. <li><a class="reference internal" href="#imaginary-literals">2.4.7. Imaginary literals</a></li>
  116. </ul>
  117. </li>
  118. <li><a class="reference internal" href="#operators">2.5. Operators</a></li>
  119. <li><a class="reference internal" href="#delimiters">2.6. Delimiters</a></li>
  120. </ul>
  121. </li>
  122. </ul>
  123. </div>
  124. <div>
  125. <h4>Previous topic</h4>
  126. <p class="topless"><a href="introduction.html"
  127. title="previous chapter"><span class="section-number">1. </span>Introduction</a></p>
  128. </div>
  129. <div>
  130. <h4>Next topic</h4>
  131. <p class="topless"><a href="datamodel.html"
  132. title="next chapter"><span class="section-number">3. </span>Data model</a></p>
  133. </div>
  134. <div role="note" aria-label="source link">
  135. <h3>This Page</h3>
  136. <ul class="this-page-menu">
  137. <li><a href="../bugs.html">Report a Bug</a></li>
  138. <li>
  139. <a href="https://github.com/python/cpython/blob/main/Doc/reference/lexical_analysis.rst"
  140. rel="nofollow">Show Source
  141. </a>
  142. </li>
  143. </ul>
  144. </div>
  145. </nav>
  146. </div>
  147. </div>
  148. <div class="related" role="navigation" aria-label="related navigation">
  149. <h3>Navigation</h3>
  150. <ul>
  151. <li class="right" style="margin-right: 10px">
  152. <a href="../genindex.html" title="General Index"
  153. accesskey="I">index</a></li>
  154. <li class="right" >
  155. <a href="../py-modindex.html" title="Python Module Index"
  156. >modules</a> |</li>
  157. <li class="right" >
  158. <a href="datamodel.html" title="3. Data model"
  159. accesskey="N">next</a> |</li>
  160. <li class="right" >
  161. <a href="introduction.html" title="1. Introduction"
  162. accesskey="P">previous</a> |</li>
  163. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  164. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  165. <li class="switchers">
  166. <div class="language_switcher_placeholder"></div>
  167. <div class="version_switcher_placeholder"></div>
  168. </li>
  169. <li>
  170. </li>
  171. <li id="cpython-language-and-version">
  172. <a href="../index.html">3.12.3 Documentation</a> &#187;
  173. </li>
  174. <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Language Reference</a> &#187;</li>
  175. <li class="nav-item nav-item-this"><a href=""><span class="section-number">2. </span>Lexical analysis</a></li>
  176. <li class="right">
  177. <div class="inline-search" role="search">
  178. <form class="inline-search" action="../search.html" method="get">
  179. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  180. <input type="submit" value="Go" />
  181. </form>
  182. </div>
  183. |
  184. </li>
  185. <li class="right">
  186. <label class="theme-selector-label">
  187. Theme
  188. <select class="theme-selector" oninput="activateTheme(this.value)">
  189. <option value="auto" selected>Auto</option>
  190. <option value="light">Light</option>
  191. <option value="dark">Dark</option>
  192. </select>
  193. </label> |</li>
  194. </ul>
  195. </div>
  196. <div class="document">
  197. <div class="documentwrapper">
  198. <div class="bodywrapper">
  199. <div class="body" role="main">
  200. <section id="lexical-analysis">
  201. <span id="lexical"></span><h1><span class="section-number">2. </span>Lexical analysis<a class="headerlink" href="#lexical-analysis" title="Link to this heading">¶</a></h1>
  202. <p id="index-0">A Python program is read by a <em>parser</em>. Input to the parser is a stream of
  203. <em>tokens</em>, generated by the <em>lexical analyzer</em>. This chapter describes how the
  204. lexical analyzer breaks a file into tokens.</p>
  205. <p>Python reads program text as Unicode code points; the encoding of a source file
  206. can be given by an encoding declaration and defaults to UTF-8, see <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-3120/"><strong>PEP 3120</strong></a>
  207. for details. If the source file cannot be decoded, a <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> is
  208. raised.</p>
  209. <section id="line-structure">
  210. <span id="id1"></span><h2><span class="section-number">2.1. </span>Line structure<a class="headerlink" href="#line-structure" title="Link to this heading">¶</a></h2>
  211. <p id="index-2">A Python program is divided into a number of <em>logical lines</em>.</p>
  212. <section id="logical-lines">
  213. <span id="id2"></span><h3><span class="section-number">2.1.1. </span>Logical lines<a class="headerlink" href="#logical-lines" title="Link to this heading">¶</a></h3>
  214. <p id="index-3">The end of a logical line is represented by the token NEWLINE. Statements
  215. cannot cross logical line boundaries except where NEWLINE is allowed by the
  216. syntax (e.g., between statements in compound statements). A logical line is
  217. constructed from one or more <em>physical lines</em> by following the explicit or
  218. implicit <em>line joining</em> rules.</p>
  219. </section>
  220. <section id="physical-lines">
  221. <span id="id3"></span><h3><span class="section-number">2.1.2. </span>Physical lines<a class="headerlink" href="#physical-lines" title="Link to this heading">¶</a></h3>
  222. <p>A physical line is a sequence of characters terminated by an end-of-line
  223. sequence. In source files and strings, any of the standard platform line
  224. termination sequences can be used - the Unix form using ASCII LF (linefeed),
  225. the Windows form using the ASCII sequence CR LF (return followed by linefeed),
  226. or the old Macintosh form using the ASCII CR (return) character. All of these
  227. forms can be used equally, regardless of platform. The end of input also serves
  228. as an implicit terminator for the final physical line.</p>
  229. <p>When embedding Python, source code strings should be passed to Python APIs using
  230. the standard C conventions for newline characters (the <code class="docutils literal notranslate"><span class="pre">\n</span></code> character,
  231. representing ASCII LF, is the line terminator).</p>
  232. </section>
  233. <section id="comments">
  234. <span id="id4"></span><h3><span class="section-number">2.1.3. </span>Comments<a class="headerlink" href="#comments" title="Link to this heading">¶</a></h3>
  235. <p id="index-4">A comment starts with a hash character (<code class="docutils literal notranslate"><span class="pre">#</span></code>) that is not part of a string
  236. literal, and ends at the end of the physical line. A comment signifies the end
  237. of the logical line unless the implicit line joining rules are invoked. Comments
  238. are ignored by the syntax.</p>
  239. </section>
  240. <section id="encoding-declarations">
  241. <span id="encodings"></span><h3><span class="section-number">2.1.4. </span>Encoding declarations<a class="headerlink" href="#encoding-declarations" title="Link to this heading">¶</a></h3>
  242. <p id="index-5">If a comment in the first or second line of the Python script matches the
  243. regular expression <code class="docutils literal notranslate"><span class="pre">coding[=:]\s*([-\w.]+)</span></code>, this comment is processed as an
  244. encoding declaration; the first group of this expression names the encoding of
  245. the source code file. The encoding declaration must appear on a line of its
  246. own. If it is the second line, the first line must also be a comment-only line.
  247. The recommended forms of an encoding expression are</p>
  248. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># -*- coding: &lt;encoding-name&gt; -*-</span>
  249. </pre></div>
  250. </div>
  251. <p>which is recognized also by GNU Emacs, and</p>
  252. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># vim:fileencoding=&lt;encoding-name&gt;</span>
  253. </pre></div>
  254. </div>
  255. <p>which is recognized by Bram Moolenaar’s VIM.</p>
  256. <p>If no encoding declaration is found, the default encoding is UTF-8. If the
  257. implicit or explicit encoding of a file is UTF-8, an initial UTF-8 byte-order
  258. mark (b’xefxbbxbf’) is ignored rather than being a syntax error.</p>
  259. <p>If an encoding is declared, the encoding name must be recognized by Python
  260. (see <a class="reference internal" href="../library/codecs.html#standard-encodings"><span class="std std-ref">Standard Encodings</span></a>). The
  261. encoding is used for all lexical analysis, including string literals, comments
  262. and identifiers.</p>
  263. </section>
  264. <section id="explicit-line-joining">
  265. <span id="explicit-joining"></span><h3><span class="section-number">2.1.5. </span>Explicit line joining<a class="headerlink" href="#explicit-line-joining" title="Link to this heading">¶</a></h3>
  266. <p id="index-6">Two or more physical lines may be joined into logical lines using backslash
  267. characters (<code class="docutils literal notranslate"><span class="pre">\</span></code>), as follows: when a physical line ends in a backslash that is
  268. not part of a string literal or comment, it is joined with the following forming
  269. a single logical line, deleting the backslash and the following end-of-line
  270. character. For example:</p>
  271. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="mi">1900</span> <span class="o">&lt;</span> <span class="n">year</span> <span class="o">&lt;</span> <span class="mi">2100</span> <span class="ow">and</span> <span class="mi">1</span> <span class="o">&lt;=</span> <span class="n">month</span> <span class="o">&lt;=</span> <span class="mi">12</span> \
  272. <span class="ow">and</span> <span class="mi">1</span> <span class="o">&lt;=</span> <span class="n">day</span> <span class="o">&lt;=</span> <span class="mi">31</span> <span class="ow">and</span> <span class="mi">0</span> <span class="o">&lt;=</span> <span class="n">hour</span> <span class="o">&lt;</span> <span class="mi">24</span> \
  273. <span class="ow">and</span> <span class="mi">0</span> <span class="o">&lt;=</span> <span class="n">minute</span> <span class="o">&lt;</span> <span class="mi">60</span> <span class="ow">and</span> <span class="mi">0</span> <span class="o">&lt;=</span> <span class="n">second</span> <span class="o">&lt;</span> <span class="mi">60</span><span class="p">:</span> <span class="c1"># Looks like a valid date</span>
  274. <span class="k">return</span> <span class="mi">1</span>
  275. </pre></div>
  276. </div>
  277. <p>A line ending in a backslash cannot carry a comment. A backslash does not
  278. continue a comment. A backslash does not continue a token except for string
  279. literals (i.e., tokens other than string literals cannot be split across
  280. physical lines using a backslash). A backslash is illegal elsewhere on a line
  281. outside a string literal.</p>
  282. </section>
  283. <section id="implicit-line-joining">
  284. <span id="implicit-joining"></span><h3><span class="section-number">2.1.6. </span>Implicit line joining<a class="headerlink" href="#implicit-line-joining" title="Link to this heading">¶</a></h3>
  285. <p>Expressions in parentheses, square brackets or curly braces can be split over
  286. more than one physical line without using backslashes. For example:</p>
  287. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">month_names</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Januari&#39;</span><span class="p">,</span> <span class="s1">&#39;Februari&#39;</span><span class="p">,</span> <span class="s1">&#39;Maart&#39;</span><span class="p">,</span> <span class="c1"># These are the</span>
  288. <span class="s1">&#39;April&#39;</span><span class="p">,</span> <span class="s1">&#39;Mei&#39;</span><span class="p">,</span> <span class="s1">&#39;Juni&#39;</span><span class="p">,</span> <span class="c1"># Dutch names</span>
  289. <span class="s1">&#39;Juli&#39;</span><span class="p">,</span> <span class="s1">&#39;Augustus&#39;</span><span class="p">,</span> <span class="s1">&#39;September&#39;</span><span class="p">,</span> <span class="c1"># for the months</span>
  290. <span class="s1">&#39;Oktober&#39;</span><span class="p">,</span> <span class="s1">&#39;November&#39;</span><span class="p">,</span> <span class="s1">&#39;December&#39;</span><span class="p">]</span> <span class="c1"># of the year</span>
  291. </pre></div>
  292. </div>
  293. <p>Implicitly continued lines can carry comments. The indentation of the
  294. continuation lines is not important. Blank continuation lines are allowed.
  295. There is no NEWLINE token between implicit continuation lines. Implicitly
  296. continued lines can also occur within triple-quoted strings (see below); in that
  297. case they cannot carry comments.</p>
  298. </section>
  299. <section id="blank-lines">
  300. <span id="id5"></span><h3><span class="section-number">2.1.7. </span>Blank lines<a class="headerlink" href="#blank-lines" title="Link to this heading">¶</a></h3>
  301. <p id="index-7">A logical line that contains only spaces, tabs, formfeeds and possibly a
  302. comment, is ignored (i.e., no NEWLINE token is generated). During interactive
  303. input of statements, handling of a blank line may differ depending on the
  304. implementation of the read-eval-print loop. In the standard interactive
  305. interpreter, an entirely blank logical line (i.e. one containing not even
  306. whitespace or a comment) terminates a multi-line statement.</p>
  307. </section>
  308. <section id="indentation">
  309. <span id="id6"></span><h3><span class="section-number">2.1.8. </span>Indentation<a class="headerlink" href="#indentation" title="Link to this heading">¶</a></h3>
  310. <p id="index-8">Leading whitespace (spaces and tabs) at the beginning of a logical line is used
  311. to compute the indentation level of the line, which in turn is used to determine
  312. the grouping of statements.</p>
  313. <p>Tabs are replaced (from left to right) by one to eight spaces such that the
  314. total number of characters up to and including the replacement is a multiple of
  315. eight (this is intended to be the same rule as used by Unix). The total number
  316. of spaces preceding the first non-blank character then determines the line’s
  317. indentation. Indentation cannot be split over multiple physical lines using
  318. backslashes; the whitespace up to the first backslash determines the
  319. indentation.</p>
  320. <p>Indentation is rejected as inconsistent if a source file mixes tabs and spaces
  321. in a way that makes the meaning dependent on the worth of a tab in spaces; a
  322. <a class="reference internal" href="../library/exceptions.html#TabError" title="TabError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TabError</span></code></a> is raised in that case.</p>
  323. <p><strong>Cross-platform compatibility note:</strong> because of the nature of text editors on
  324. non-UNIX platforms, it is unwise to use a mixture of spaces and tabs for the
  325. indentation in a single source file. It should also be noted that different
  326. platforms may explicitly limit the maximum indentation level.</p>
  327. <p>A formfeed character may be present at the start of the line; it will be ignored
  328. for the indentation calculations above. Formfeed characters occurring elsewhere
  329. in the leading whitespace have an undefined effect (for instance, they may reset
  330. the space count to zero).</p>
  331. <p id="index-9">The indentation levels of consecutive lines are used to generate INDENT and
  332. DEDENT tokens, using a stack, as follows.</p>
  333. <p>Before the first line of the file is read, a single zero is pushed on the stack;
  334. this will never be popped off again. The numbers pushed on the stack will
  335. always be strictly increasing from bottom to top. At the beginning of each
  336. logical line, the line’s indentation level is compared to the top of the stack.
  337. If it is equal, nothing happens. If it is larger, it is pushed on the stack, and
  338. one INDENT token is generated. If it is smaller, it <em>must</em> be one of the
  339. numbers occurring on the stack; all numbers on the stack that are larger are
  340. popped off, and for each number popped off a DEDENT token is generated. At the
  341. end of the file, a DEDENT token is generated for each number remaining on the
  342. stack that is larger than zero.</p>
  343. <p>Here is an example of a correctly (though confusingly) indented piece of Python
  344. code:</p>
  345. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">perm</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
  346. <span class="c1"># Compute the list of all permutations of l</span>
  347. <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">:</span>
  348. <span class="k">return</span> <span class="p">[</span><span class="n">l</span><span class="p">]</span>
  349. <span class="n">r</span> <span class="o">=</span> <span class="p">[]</span>
  350. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="p">)):</span>
  351. <span class="n">s</span> <span class="o">=</span> <span class="n">l</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
  352. <span class="n">p</span> <span class="o">=</span> <span class="n">perm</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
  353. <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span>
  354. <span class="n">r</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span>
  355. <span class="k">return</span> <span class="n">r</span>
  356. </pre></div>
  357. </div>
  358. <p>The following example shows various indentation errors:</p>
  359. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span> <span class="k">def</span> <span class="nf">perm</span><span class="p">(</span><span class="n">l</span><span class="p">):</span> <span class="c1"># error: first line indented</span>
  360. <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">l</span><span class="p">)):</span> <span class="c1"># error: not indented</span>
  361. <span class="n">s</span> <span class="o">=</span> <span class="n">l</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
  362. <span class="n">p</span> <span class="o">=</span> <span class="n">perm</span><span class="p">(</span><span class="n">l</span><span class="p">[:</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">:])</span> <span class="c1"># error: unexpected indent</span>
  363. <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span>
  364. <span class="n">r</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">l</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="n">x</span><span class="p">)</span>
  365. <span class="k">return</span> <span class="n">r</span> <span class="c1"># error: inconsistent dedent</span>
  366. </pre></div>
  367. </div>
  368. <p>(Actually, the first three errors are detected by the parser; only the last
  369. error is found by the lexical analyzer — the indentation of <code class="docutils literal notranslate"><span class="pre">return</span> <span class="pre">r</span></code> does
  370. not match a level popped off the stack.)</p>
  371. </section>
  372. <section id="whitespace-between-tokens">
  373. <span id="whitespace"></span><h3><span class="section-number">2.1.9. </span>Whitespace between tokens<a class="headerlink" href="#whitespace-between-tokens" title="Link to this heading">¶</a></h3>
  374. <p>Except at the beginning of a logical line or in string literals, the whitespace
  375. characters space, tab and formfeed can be used interchangeably to separate
  376. tokens. Whitespace is needed between two tokens only if their concatenation
  377. could otherwise be interpreted as a different token (e.g., ab is one token, but
  378. a b is two tokens).</p>
  379. </section>
  380. </section>
  381. <section id="other-tokens">
  382. <span id="id7"></span><h2><span class="section-number">2.2. </span>Other tokens<a class="headerlink" href="#other-tokens" title="Link to this heading">¶</a></h2>
  383. <p>Besides NEWLINE, INDENT and DEDENT, the following categories of tokens exist:
  384. <em>identifiers</em>, <em>keywords</em>, <em>literals</em>, <em>operators</em>, and <em>delimiters</em>. Whitespace
  385. characters (other than line terminators, discussed earlier) are not tokens, but
  386. serve to delimit tokens. Where ambiguity exists, a token comprises the longest
  387. possible string that forms a legal token, when read from left to right.</p>
  388. </section>
  389. <section id="identifiers">
  390. <span id="identifiers-and-keywords"></span><h2><span class="section-number">2.3. </span>Identifiers and keywords<a class="headerlink" href="#identifiers" title="Link to this heading">¶</a></h2>
  391. <p id="index-10">Identifiers (also referred to as <em>names</em>) are described by the following lexical
  392. definitions.</p>
  393. <p>The syntax of identifiers in Python is based on the Unicode standard annex
  394. UAX-31, with elaboration and changes as defined below; see also <span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-3131/"><strong>PEP 3131</strong></a> for
  395. further details.</p>
  396. <p>Within the ASCII range (U+0001..U+007F), the valid characters for identifiers
  397. are the same as in Python 2.x: the uppercase and lowercase letters <code class="docutils literal notranslate"><span class="pre">A</span></code> through
  398. <code class="docutils literal notranslate"><span class="pre">Z</span></code>, the underscore <code class="docutils literal notranslate"><span class="pre">_</span></code> and, except for the first character, the digits
  399. <code class="docutils literal notranslate"><span class="pre">0</span></code> through <code class="docutils literal notranslate"><span class="pre">9</span></code>.</p>
  400. <p>Python 3.0 introduces additional characters from outside the ASCII range (see
  401. <span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-3131/"><strong>PEP 3131</strong></a>). For these characters, the classification uses the version of the
  402. Unicode Character Database as included in the <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><code class="xref py py-mod docutils literal notranslate"><span class="pre">unicodedata</span></code></a> module.</p>
  403. <p>Identifiers are unlimited in length. Case is significant.</p>
  404. <pre>
  405. <strong id="grammar-token-python-grammar-identifier">identifier </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-xid_start"><code class="xref docutils literal notranslate"><span class="pre">xid_start</span></code></a> <a class="reference internal" href="#grammar-token-python-grammar-xid_continue"><code class="xref docutils literal notranslate"><span class="pre">xid_continue</span></code></a>*
  406. <strong id="grammar-token-python-grammar-id_start">id_start </strong> ::= &lt;all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property&gt;
  407. <strong id="grammar-token-python-grammar-id_continue">id_continue </strong> ::= &lt;all characters in <a class="reference internal" href="#grammar-token-python-grammar-id_start"><code class="xref docutils literal notranslate"><span class="pre">id_start</span></code></a>, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property&gt;
  408. <strong id="grammar-token-python-grammar-xid_start">xid_start </strong> ::= &lt;all characters in <a class="reference internal" href="#grammar-token-python-grammar-id_start"><code class="xref docutils literal notranslate"><span class="pre">id_start</span></code></a> whose NFKC normalization is in &quot;id_start xid_continue*&quot;&gt;
  409. <strong id="grammar-token-python-grammar-xid_continue">xid_continue</strong> ::= &lt;all characters in <a class="reference internal" href="#grammar-token-python-grammar-id_continue"><code class="xref docutils literal notranslate"><span class="pre">id_continue</span></code></a> whose NFKC normalization is in &quot;id_continue*&quot;&gt;
  410. </pre>
  411. <p>The Unicode category codes mentioned above stand for:</p>
  412. <ul class="simple">
  413. <li><p><em>Lu</em> - uppercase letters</p></li>
  414. <li><p><em>Ll</em> - lowercase letters</p></li>
  415. <li><p><em>Lt</em> - titlecase letters</p></li>
  416. <li><p><em>Lm</em> - modifier letters</p></li>
  417. <li><p><em>Lo</em> - other letters</p></li>
  418. <li><p><em>Nl</em> - letter numbers</p></li>
  419. <li><p><em>Mn</em> - nonspacing marks</p></li>
  420. <li><p><em>Mc</em> - spacing combining marks</p></li>
  421. <li><p><em>Nd</em> - decimal numbers</p></li>
  422. <li><p><em>Pc</em> - connector punctuations</p></li>
  423. <li><p><em>Other_ID_Start</em> - explicit list of characters in <a class="reference external" href="https://www.unicode.org/Public/15.0.0/ucd/PropList.txt">PropList.txt</a> to support backwards
  424. compatibility</p></li>
  425. <li><p><em>Other_ID_Continue</em> - likewise</p></li>
  426. </ul>
  427. <p>All identifiers are converted into the normal form NFKC while parsing; comparison
  428. of identifiers is based on NFKC.</p>
  429. <p>A non-normative HTML file listing all valid identifier characters for Unicode
  430. 15.0.0 can be found at
  431. <a class="reference external" href="https://www.unicode.org/Public/15.0.0/ucd/DerivedCoreProperties.txt">https://www.unicode.org/Public/15.0.0/ucd/DerivedCoreProperties.txt</a></p>
  432. <section id="keywords">
  433. <span id="id8"></span><h3><span class="section-number">2.3.1. </span>Keywords<a class="headerlink" href="#keywords" title="Link to this heading">¶</a></h3>
  434. <p id="index-13">The following identifiers are used as reserved words, or <em>keywords</em> of the
  435. language, and cannot be used as ordinary identifiers. They must be spelled
  436. exactly as written here:</p>
  437. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>False await else import pass
  438. None break except in raise
  439. True class finally is return
  440. and continue for lambda try
  441. as def from nonlocal while
  442. assert del global not with
  443. async elif if or yield
  444. </pre></div>
  445. </div>
  446. </section>
  447. <section id="soft-keywords">
  448. <span id="id9"></span><h3><span class="section-number">2.3.2. </span>Soft Keywords<a class="headerlink" href="#soft-keywords" title="Link to this heading">¶</a></h3>
  449. <div class="versionadded" id="index-14">
  450. <p><span class="versionmodified added">New in version 3.10.</span></p>
  451. </div>
  452. <p>Some identifiers are only reserved under specific contexts. These are known as
  453. <em>soft keywords</em>. The identifiers <code class="docutils literal notranslate"><span class="pre">match</span></code>, <code class="docutils literal notranslate"><span class="pre">case</span></code>, <code class="docutils literal notranslate"><span class="pre">type</span></code> and <code class="docutils literal notranslate"><span class="pre">_</span></code> can
  454. syntactically act as keywords in certain contexts,
  455. but this distinction is done at the parser level, not when tokenizing.</p>
  456. <p>As soft keywords, their use in the grammar is possible while still
  457. preserving compatibility with existing code that uses these names as
  458. identifier names.</p>
  459. <p><code class="docutils literal notranslate"><span class="pre">match</span></code>, <code class="docutils literal notranslate"><span class="pre">case</span></code>, and <code class="docutils literal notranslate"><span class="pre">_</span></code> are used in the <a class="reference internal" href="compound_stmts.html#match"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">match</span></code></a> statement.
  460. <code class="docutils literal notranslate"><span class="pre">type</span></code> is used in the <a class="reference internal" href="simple_stmts.html#type"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">type</span></code></a> statement.</p>
  461. <div class="versionchanged">
  462. <p><span class="versionmodified changed">Changed in version 3.12: </span><code class="docutils literal notranslate"><span class="pre">type</span></code> is now a soft keyword.</p>
  463. </div>
  464. </section>
  465. <section id="reserved-classes-of-identifiers">
  466. <span id="id-classes"></span><span id="index-15"></span><h3><span class="section-number">2.3.3. </span>Reserved classes of identifiers<a class="headerlink" href="#reserved-classes-of-identifiers" title="Link to this heading">¶</a></h3>
  467. <p>Certain classes of identifiers (besides keywords) have special meanings. These
  468. classes are identified by the patterns of leading and trailing underscore
  469. characters:</p>
  470. <dl>
  471. <dt><code class="docutils literal notranslate"><span class="pre">_*</span></code></dt><dd><p>Not imported by <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">module</span> <span class="pre">import</span> <span class="pre">*</span></code>.</p>
  472. </dd>
  473. <dt><code class="docutils literal notranslate"><span class="pre">_</span></code></dt><dd><p>In a <code class="docutils literal notranslate"><span class="pre">case</span></code> pattern within a <a class="reference internal" href="compound_stmts.html#match"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">match</span></code></a> statement, <code class="docutils literal notranslate"><span class="pre">_</span></code> is a
  474. <a class="reference internal" href="#soft-keywords"><span class="std std-ref">soft keyword</span></a> that denotes a
  475. <a class="reference internal" href="compound_stmts.html#wildcard-patterns"><span class="std std-ref">wildcard</span></a>.</p>
  476. <p>Separately, the interactive interpreter makes the result of the last evaluation
  477. available in the variable <code class="docutils literal notranslate"><span class="pre">_</span></code>.
  478. (It is stored in the <a class="reference internal" href="../library/builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><code class="xref py py-mod docutils literal notranslate"><span class="pre">builtins</span></code></a> module, alongside built-in
  479. functions like <code class="docutils literal notranslate"><span class="pre">print</span></code>.)</p>
  480. <p>Elsewhere, <code class="docutils literal notranslate"><span class="pre">_</span></code> is a regular identifier. It is often used to name
  481. “special” items, but it is not special to Python itself.</p>
  482. <div class="admonition note">
  483. <p class="admonition-title">Note</p>
  484. <p>The name <code class="docutils literal notranslate"><span class="pre">_</span></code> is often used in conjunction with internationalization;
  485. refer to the documentation for the <a class="reference internal" href="../library/gettext.html#module-gettext" title="gettext: Multilingual internationalization services."><code class="xref py py-mod docutils literal notranslate"><span class="pre">gettext</span></code></a> module for more
  486. information on this convention.</p>
  487. <p>It is also commonly used for unused variables.</p>
  488. </div>
  489. </dd>
  490. <dt><code class="docutils literal notranslate"><span class="pre">__*__</span></code></dt><dd><p>System-defined names, informally known as “dunder” names. These names are
  491. defined by the interpreter and its implementation (including the standard library).
  492. Current system names are discussed in the <a class="reference internal" href="datamodel.html#specialnames"><span class="std std-ref">Special method names</span></a> section and elsewhere.
  493. More will likely be defined in future versions of Python. <em>Any</em> use of <code class="docutils literal notranslate"><span class="pre">__*__</span></code> names,
  494. in any context, that does not follow explicitly documented use, is subject to
  495. breakage without warning.</p>
  496. </dd>
  497. <dt><code class="docutils literal notranslate"><span class="pre">__*</span></code></dt><dd><p>Class-private names. Names in this category, when used within the context of a
  498. class definition, are re-written to use a mangled form to help avoid name
  499. clashes between “private” attributes of base and derived classes. See section
  500. <a class="reference internal" href="expressions.html#atom-identifiers"><span class="std std-ref">Identifiers (Names)</span></a>.</p>
  501. </dd>
  502. </dl>
  503. </section>
  504. </section>
  505. <section id="literals">
  506. <span id="id10"></span><h2><span class="section-number">2.4. </span>Literals<a class="headerlink" href="#literals" title="Link to this heading">¶</a></h2>
  507. <p id="index-16">Literals are notations for constant values of some built-in types.</p>
  508. <section id="string-and-bytes-literals">
  509. <span id="strings"></span><span id="index-17"></span><h3><span class="section-number">2.4.1. </span>String and Bytes literals<a class="headerlink" href="#string-and-bytes-literals" title="Link to this heading">¶</a></h3>
  510. <p>String literals are described by the following lexical definitions:</p>
  511. <pre>
  512. <strong id="grammar-token-python-grammar-stringliteral">stringliteral </strong> ::= [<a class="reference internal" href="#grammar-token-python-grammar-stringprefix"><code class="xref docutils literal notranslate"><span class="pre">stringprefix</span></code></a>](<a class="reference internal" href="#grammar-token-python-grammar-shortstring"><code class="xref docutils literal notranslate"><span class="pre">shortstring</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-longstring"><code class="xref docutils literal notranslate"><span class="pre">longstring</span></code></a>)
  513. <strong id="grammar-token-python-grammar-stringprefix">stringprefix </strong> ::= &quot;r&quot; | &quot;u&quot; | &quot;R&quot; | &quot;U&quot; | &quot;f&quot; | &quot;F&quot;
  514. | &quot;fr&quot; | &quot;Fr&quot; | &quot;fR&quot; | &quot;FR&quot; | &quot;rf&quot; | &quot;rF&quot; | &quot;Rf&quot; | &quot;RF&quot;
  515. <strong id="grammar-token-python-grammar-shortstring">shortstring </strong> ::= &quot;'&quot; <a class="reference internal" href="#grammar-token-python-grammar-shortstringitem"><code class="xref docutils literal notranslate"><span class="pre">shortstringitem</span></code></a>* &quot;'&quot; | '&quot;' <a class="reference internal" href="#grammar-token-python-grammar-shortstringitem"><code class="xref docutils literal notranslate"><span class="pre">shortstringitem</span></code></a>* '&quot;'
  516. <strong id="grammar-token-python-grammar-longstring">longstring </strong> ::= &quot;'''&quot; <a class="reference internal" href="#grammar-token-python-grammar-longstringitem"><code class="xref docutils literal notranslate"><span class="pre">longstringitem</span></code></a>* &quot;'''&quot; | '&quot;&quot;&quot;' <a class="reference internal" href="#grammar-token-python-grammar-longstringitem"><code class="xref docutils literal notranslate"><span class="pre">longstringitem</span></code></a>* '&quot;&quot;&quot;'
  517. <strong id="grammar-token-python-grammar-shortstringitem">shortstringitem</strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-shortstringchar"><code class="xref docutils literal notranslate"><span class="pre">shortstringchar</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-stringescapeseq"><code class="xref docutils literal notranslate"><span class="pre">stringescapeseq</span></code></a>
  518. <strong id="grammar-token-python-grammar-longstringitem">longstringitem </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-longstringchar"><code class="xref docutils literal notranslate"><span class="pre">longstringchar</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-stringescapeseq"><code class="xref docutils literal notranslate"><span class="pre">stringescapeseq</span></code></a>
  519. <strong id="grammar-token-python-grammar-shortstringchar">shortstringchar</strong> ::= &lt;any source character except &quot;\&quot; or newline or the quote&gt;
  520. <strong id="grammar-token-python-grammar-longstringchar">longstringchar </strong> ::= &lt;any source character except &quot;\&quot;&gt;
  521. <strong id="grammar-token-python-grammar-stringescapeseq">stringescapeseq</strong> ::= &quot;\&quot; &lt;any source character&gt;
  522. </pre>
  523. <pre>
  524. <strong id="grammar-token-python-grammar-bytesliteral">bytesliteral </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-bytesprefix"><code class="xref docutils literal notranslate"><span class="pre">bytesprefix</span></code></a>(<a class="reference internal" href="#grammar-token-python-grammar-shortbytes"><code class="xref docutils literal notranslate"><span class="pre">shortbytes</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-longbytes"><code class="xref docutils literal notranslate"><span class="pre">longbytes</span></code></a>)
  525. <strong id="grammar-token-python-grammar-bytesprefix">bytesprefix </strong> ::= &quot;b&quot; | &quot;B&quot; | &quot;br&quot; | &quot;Br&quot; | &quot;bR&quot; | &quot;BR&quot; | &quot;rb&quot; | &quot;rB&quot; | &quot;Rb&quot; | &quot;RB&quot;
  526. <strong id="grammar-token-python-grammar-shortbytes">shortbytes </strong> ::= &quot;'&quot; <a class="reference internal" href="#grammar-token-python-grammar-shortbytesitem"><code class="xref docutils literal notranslate"><span class="pre">shortbytesitem</span></code></a>* &quot;'&quot; | '&quot;' <a class="reference internal" href="#grammar-token-python-grammar-shortbytesitem"><code class="xref docutils literal notranslate"><span class="pre">shortbytesitem</span></code></a>* '&quot;'
  527. <strong id="grammar-token-python-grammar-longbytes">longbytes </strong> ::= &quot;'''&quot; <a class="reference internal" href="#grammar-token-python-grammar-longbytesitem"><code class="xref docutils literal notranslate"><span class="pre">longbytesitem</span></code></a>* &quot;'''&quot; | '&quot;&quot;&quot;' <a class="reference internal" href="#grammar-token-python-grammar-longbytesitem"><code class="xref docutils literal notranslate"><span class="pre">longbytesitem</span></code></a>* '&quot;&quot;&quot;'
  528. <strong id="grammar-token-python-grammar-shortbytesitem">shortbytesitem</strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-shortbyteschar"><code class="xref docutils literal notranslate"><span class="pre">shortbyteschar</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-bytesescapeseq"><code class="xref docutils literal notranslate"><span class="pre">bytesescapeseq</span></code></a>
  529. <strong id="grammar-token-python-grammar-longbytesitem">longbytesitem </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-longbyteschar"><code class="xref docutils literal notranslate"><span class="pre">longbyteschar</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-bytesescapeseq"><code class="xref docutils literal notranslate"><span class="pre">bytesescapeseq</span></code></a>
  530. <strong id="grammar-token-python-grammar-shortbyteschar">shortbyteschar</strong> ::= &lt;any ASCII character except &quot;\&quot; or newline or the quote&gt;
  531. <strong id="grammar-token-python-grammar-longbyteschar">longbyteschar </strong> ::= &lt;any ASCII character except &quot;\&quot;&gt;
  532. <strong id="grammar-token-python-grammar-bytesescapeseq">bytesescapeseq</strong> ::= &quot;\&quot; &lt;any ASCII character&gt;
  533. </pre>
  534. <p>One syntactic restriction not indicated by these productions is that whitespace
  535. is not allowed between the <a class="reference internal" href="#grammar-token-python-grammar-stringprefix"><code class="xref std std-token docutils literal notranslate"><span class="pre">stringprefix</span></code></a> or
  536. <a class="reference internal" href="#grammar-token-python-grammar-bytesprefix"><code class="xref std std-token docutils literal notranslate"><span class="pre">bytesprefix</span></code></a> and the rest of the literal. The source
  537. character set is defined by the encoding declaration; it is UTF-8 if no encoding
  538. declaration is given in the source file; see section <a class="reference internal" href="#encodings"><span class="std std-ref">Encoding declarations</span></a>.</p>
  539. <p id="index-18">In plain English: Both types of literals can be enclosed in matching single quotes
  540. (<code class="docutils literal notranslate"><span class="pre">'</span></code>) or double quotes (<code class="docutils literal notranslate"><span class="pre">&quot;</span></code>). They can also be enclosed in matching groups
  541. of three single or double quotes (these are generally referred to as
  542. <em>triple-quoted strings</em>). The backslash (<code class="docutils literal notranslate"><span class="pre">\</span></code>) character is used to give special
  543. meaning to otherwise ordinary characters like <code class="docutils literal notranslate"><span class="pre">n</span></code>, which means ‘newline’ when
  544. escaped (<code class="docutils literal notranslate"><span class="pre">\n</span></code>). It can also be used to escape characters that otherwise have a
  545. special meaning, such as newline, backslash itself, or the quote character.
  546. See <a class="reference internal" href="#escape-sequences"><span class="std std-ref">escape sequences</span></a> below for examples.</p>
  547. <p id="index-19">Bytes literals are always prefixed with <code class="docutils literal notranslate"><span class="pre">'b'</span></code> or <code class="docutils literal notranslate"><span class="pre">'B'</span></code>; they produce an
  548. instance of the <a class="reference internal" href="../library/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> type instead of the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> type. They
  549. may only contain ASCII characters; bytes with a numeric value of 128 or greater
  550. must be expressed with escapes.</p>
  551. <p id="index-20">Both string and bytes literals may optionally be prefixed with a letter <code class="docutils literal notranslate"><span class="pre">'r'</span></code>
  552. or <code class="docutils literal notranslate"><span class="pre">'R'</span></code>; such strings are called <em class="dfn">raw strings</em> and treat backslashes as
  553. literal characters. As a result, in string literals, <code class="docutils literal notranslate"><span class="pre">'\U'</span></code> and <code class="docutils literal notranslate"><span class="pre">'\u'</span></code>
  554. escapes in raw strings are not treated specially. Given that Python 2.x’s raw
  555. unicode literals behave differently than Python 3.x’s the <code class="docutils literal notranslate"><span class="pre">'ur'</span></code> syntax
  556. is not supported.</p>
  557. <div class="versionadded">
  558. <p><span class="versionmodified added">New in version 3.3: </span>The <code class="docutils literal notranslate"><span class="pre">'rb'</span></code> prefix of raw bytes literals has been added as a synonym
  559. of <code class="docutils literal notranslate"><span class="pre">'br'</span></code>.</p>
  560. <p>Support for the unicode legacy literal (<code class="docutils literal notranslate"><span class="pre">u'value'</span></code>) was reintroduced
  561. to simplify the maintenance of dual Python 2.x and 3.x codebases.
  562. See <span class="target" id="index-21"></span><a class="pep reference external" href="https://peps.python.org/pep-0414/"><strong>PEP 414</strong></a> for more information.</p>
  563. </div>
  564. <p id="index-22">A string literal with <code class="docutils literal notranslate"><span class="pre">'f'</span></code> or <code class="docutils literal notranslate"><span class="pre">'F'</span></code> in its prefix is a
  565. <em class="dfn">formatted string literal</em>; see <a class="reference internal" href="#f-strings"><span class="std std-ref">f-strings</span></a>. The <code class="docutils literal notranslate"><span class="pre">'f'</span></code> may be
  566. combined with <code class="docutils literal notranslate"><span class="pre">'r'</span></code>, but not with <code class="docutils literal notranslate"><span class="pre">'b'</span></code> or <code class="docutils literal notranslate"><span class="pre">'u'</span></code>, therefore raw
  567. formatted strings are possible, but formatted bytes literals are not.</p>
  568. <p>In triple-quoted literals, unescaped newlines and quotes are allowed (and are
  569. retained), except that three unescaped quotes in a row terminate the literal. (A
  570. “quote” is the character used to open the literal, i.e. either <code class="docutils literal notranslate"><span class="pre">'</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;</span></code>.)</p>
  571. <section id="escape-sequences">
  572. <span id="index-23"></span><span id="id11"></span><h4><span class="section-number">2.4.1.1. </span>Escape sequences<a class="headerlink" href="#escape-sequences" title="Link to this heading">¶</a></h4>
  573. <p>Unless an <code class="docutils literal notranslate"><span class="pre">'r'</span></code> or <code class="docutils literal notranslate"><span class="pre">'R'</span></code> prefix is present, escape sequences in string and
  574. bytes literals are interpreted according to rules similar to those used by
  575. Standard C. The recognized escape sequences are:</p>
  576. <table class="docutils align-default">
  577. <thead>
  578. <tr class="row-odd"><th class="head"><p>Escape Sequence</p></th>
  579. <th class="head"><p>Meaning</p></th>
  580. <th class="head"><p>Notes</p></th>
  581. </tr>
  582. </thead>
  583. <tbody>
  584. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\</span></code>&lt;newline&gt;</p></td>
  585. <td><p>Backslash and newline ignored</p></td>
  586. <td><p>(1)</p></td>
  587. </tr>
  588. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\\</span></code></p></td>
  589. <td><p>Backslash (<code class="docutils literal notranslate"><span class="pre">\</span></code>)</p></td>
  590. <td></td>
  591. </tr>
  592. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\'</span></code></p></td>
  593. <td><p>Single quote (<code class="docutils literal notranslate"><span class="pre">'</span></code>)</p></td>
  594. <td></td>
  595. </tr>
  596. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\&quot;</span></code></p></td>
  597. <td><p>Double quote (<code class="docutils literal notranslate"><span class="pre">&quot;</span></code>)</p></td>
  598. <td></td>
  599. </tr>
  600. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\a</span></code></p></td>
  601. <td><p>ASCII Bell (BEL)</p></td>
  602. <td></td>
  603. </tr>
  604. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\b</span></code></p></td>
  605. <td><p>ASCII Backspace (BS)</p></td>
  606. <td></td>
  607. </tr>
  608. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\f</span></code></p></td>
  609. <td><p>ASCII Formfeed (FF)</p></td>
  610. <td></td>
  611. </tr>
  612. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\n</span></code></p></td>
  613. <td><p>ASCII Linefeed (LF)</p></td>
  614. <td></td>
  615. </tr>
  616. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\r</span></code></p></td>
  617. <td><p>ASCII Carriage Return (CR)</p></td>
  618. <td></td>
  619. </tr>
  620. <tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\t</span></code></p></td>
  621. <td><p>ASCII Horizontal Tab (TAB)</p></td>
  622. <td></td>
  623. </tr>
  624. <tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\v</span></code></p></td>
  625. <td><p>ASCII Vertical Tab (VT)</p></td>
  626. <td></td>
  627. </tr>
  628. <tr class="row-odd"><td><p><code class="samp docutils literal notranslate"><span class="pre">\</span><em><span class="pre">ooo</span></em></code></p></td>
  629. <td><p>Character with octal value
  630. <em>ooo</em></p></td>
  631. <td><p>(2,4)</p></td>
  632. </tr>
  633. <tr class="row-even"><td><p><code class="samp docutils literal notranslate"><span class="pre">\x</span><em><span class="pre">hh</span></em></code></p></td>
  634. <td><p>Character with hex value <em>hh</em></p></td>
  635. <td><p>(3,4)</p></td>
  636. </tr>
  637. </tbody>
  638. </table>
  639. <p>Escape sequences only recognized in string literals are:</p>
  640. <table class="docutils align-default">
  641. <thead>
  642. <tr class="row-odd"><th class="head"><p>Escape Sequence</p></th>
  643. <th class="head"><p>Meaning</p></th>
  644. <th class="head"><p>Notes</p></th>
  645. </tr>
  646. </thead>
  647. <tbody>
  648. <tr class="row-even"><td><p><code class="samp docutils literal notranslate"><span class="pre">\N{</span><em><span class="pre">name</span></em><span class="pre">}</span></code></p></td>
  649. <td><p>Character named <em>name</em> in the
  650. Unicode database</p></td>
  651. <td><p>(5)</p></td>
  652. </tr>
  653. <tr class="row-odd"><td><p><code class="samp docutils literal notranslate"><span class="pre">\u</span><em><span class="pre">xxxx</span></em></code></p></td>
  654. <td><p>Character with 16-bit hex value
  655. <em>xxxx</em></p></td>
  656. <td><p>(6)</p></td>
  657. </tr>
  658. <tr class="row-even"><td><p><code class="samp docutils literal notranslate"><span class="pre">\U</span><em><span class="pre">xxxxxxxx</span></em></code></p></td>
  659. <td><p>Character with 32-bit hex value
  660. <em>xxxxxxxx</em></p></td>
  661. <td><p>(7)</p></td>
  662. </tr>
  663. </tbody>
  664. </table>
  665. <p>Notes:</p>
  666. <ol class="arabic">
  667. <li><p>A backslash can be added at the end of a line to ignore the newline:</p>
  668. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="s1">&#39;This string will not include </span><span class="se">\</span>
  669. <span class="gp">... </span><span class="s1">backslashes or newline characters.&#39;</span>
  670. <span class="go">&#39;This string will not include backslashes or newline characters.&#39;</span>
  671. </pre></div>
  672. </div>
  673. <p>The same result can be achieved using <a class="reference internal" href="#strings"><span class="std std-ref">triple-quoted strings</span></a>,
  674. or parentheses and <a class="reference internal" href="#string-concatenation"><span class="std std-ref">string literal concatenation</span></a>.</p>
  675. </li>
  676. <li><p>As in Standard C, up to three octal digits are accepted.</p>
  677. <div class="versionchanged">
  678. <p><span class="versionmodified changed">Changed in version 3.11: </span>Octal escapes with value larger than <code class="docutils literal notranslate"><span class="pre">0o377</span></code> produce a
  679. <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>.</p>
  680. </div>
  681. <div class="versionchanged">
  682. <p><span class="versionmodified changed">Changed in version 3.12: </span>Octal escapes with value larger than <code class="docutils literal notranslate"><span class="pre">0o377</span></code> produce a
  683. <a class="reference internal" href="../library/exceptions.html#SyntaxWarning" title="SyntaxWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxWarning</span></code></a>. In a future Python version they will be eventually
  684. a <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>.</p>
  685. </div>
  686. </li>
  687. <li><p>Unlike in Standard C, exactly two hex digits are required.</p></li>
  688. <li><p>In a bytes literal, hexadecimal and octal escapes denote the byte with the
  689. given value. In a string literal, these escapes denote a Unicode character
  690. with the given value.</p></li>
  691. <li><div class="versionchanged">
  692. <p><span class="versionmodified changed">Changed in version 3.3: </span>Support for name aliases <a class="footnote-reference brackets" href="#id16" id="id12" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> has been added.</p>
  693. </div>
  694. </li>
  695. <li><p>Exactly four hex digits are required.</p></li>
  696. <li><p>Any Unicode character can be encoded this way. Exactly eight hex digits
  697. are required.</p></li>
  698. </ol>
  699. <p id="index-24">Unlike Standard C, all unrecognized escape sequences are left in the string
  700. unchanged, i.e., <em>the backslash is left in the result</em>. (This behavior is
  701. useful when debugging: if an escape sequence is mistyped, the resulting output
  702. is more easily recognized as broken.) It is also important to note that the
  703. escape sequences only recognized in string literals fall into the category of
  704. unrecognized escapes for bytes literals.</p>
  705. <div class="versionchanged">
  706. <p><span class="versionmodified changed">Changed in version 3.6: </span>Unrecognized escape sequences produce a <a class="reference internal" href="../library/exceptions.html#DeprecationWarning" title="DeprecationWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">DeprecationWarning</span></code></a>.</p>
  707. </div>
  708. <div class="versionchanged">
  709. <p><span class="versionmodified changed">Changed in version 3.12: </span>Unrecognized escape sequences produce a <a class="reference internal" href="../library/exceptions.html#SyntaxWarning" title="SyntaxWarning"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxWarning</span></code></a>. In a future
  710. Python version they will be eventually a <a class="reference internal" href="../library/exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a>.</p>
  711. </div>
  712. <p>Even in a raw literal, quotes can be escaped with a backslash, but the
  713. backslash remains in the result; for example, <code class="docutils literal notranslate"><span class="pre">r&quot;\&quot;&quot;</span></code> is a valid string
  714. literal consisting of two characters: a backslash and a double quote; <code class="docutils literal notranslate"><span class="pre">r&quot;\&quot;</span></code>
  715. is not a valid string literal (even a raw string cannot end in an odd number of
  716. backslashes). Specifically, <em>a raw literal cannot end in a single backslash</em>
  717. (since the backslash would escape the following quote character). Note also
  718. that a single backslash followed by a newline is interpreted as those two
  719. characters as part of the literal, <em>not</em> as a line continuation.</p>
  720. </section>
  721. </section>
  722. <section id="string-literal-concatenation">
  723. <span id="string-concatenation"></span><h3><span class="section-number">2.4.2. </span>String literal concatenation<a class="headerlink" href="#string-literal-concatenation" title="Link to this heading">¶</a></h3>
  724. <p>Multiple adjacent string or bytes literals (delimited by whitespace), possibly
  725. using different quoting conventions, are allowed, and their meaning is the same
  726. as their concatenation. Thus, <code class="docutils literal notranslate"><span class="pre">&quot;hello&quot;</span> <span class="pre">'world'</span></code> is equivalent to
  727. <code class="docutils literal notranslate"><span class="pre">&quot;helloworld&quot;</span></code>. This feature can be used to reduce the number of backslashes
  728. needed, to split long strings conveniently across long lines, or even to add
  729. comments to parts of strings, for example:</p>
  730. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;[A-Za-z_]&quot;</span> <span class="c1"># letter or underscore</span>
  731. <span class="s2">&quot;[A-Za-z0-9_]*&quot;</span> <span class="c1"># letter, digit or underscore</span>
  732. <span class="p">)</span>
  733. </pre></div>
  734. </div>
  735. <p>Note that this feature is defined at the syntactical level, but implemented at
  736. compile time. The ‘+’ operator must be used to concatenate string expressions
  737. at run time. Also note that literal concatenation can use different quoting
  738. styles for each component (even mixing raw strings and triple quoted strings),
  739. and formatted string literals may be concatenated with plain string literals.</p>
  740. </section>
  741. <section id="formatted-string-literals">
  742. <span id="f-strings"></span><span id="index-25"></span><span id="id13"></span><h3><span class="section-number">2.4.3. </span>f-strings<a class="headerlink" href="#formatted-string-literals" title="Link to this heading">¶</a></h3>
  743. <div class="versionadded">
  744. <p><span class="versionmodified added">New in version 3.6.</span></p>
  745. </div>
  746. <p>A <em class="dfn">formatted string literal</em> or <em class="dfn">f-string</em> is a string literal
  747. that is prefixed with <code class="docutils literal notranslate"><span class="pre">'f'</span></code> or <code class="docutils literal notranslate"><span class="pre">'F'</span></code>. These strings may contain
  748. replacement fields, which are expressions delimited by curly braces <code class="docutils literal notranslate"><span class="pre">{}</span></code>.
  749. While other string literals always have a constant value, formatted strings
  750. are really expressions evaluated at run time.</p>
  751. <p>Escape sequences are decoded like in ordinary string literals (except when
  752. a literal is also marked as a raw string). After decoding, the grammar
  753. for the contents of the string is:</p>
  754. <pre>
  755. <strong id="grammar-token-python-grammar-f_string">f_string </strong> ::= (<a class="reference internal" href="#grammar-token-python-grammar-literal_char"><code class="xref docutils literal notranslate"><span class="pre">literal_char</span></code></a> | &quot;{{&quot; | &quot;}}&quot; | <a class="reference internal" href="#grammar-token-python-grammar-replacement_field"><code class="xref docutils literal notranslate"><span class="pre">replacement_field</span></code></a>)*
  756. <strong id="grammar-token-python-grammar-replacement_field">replacement_field</strong> ::= &quot;{&quot; <a class="reference internal" href="#grammar-token-python-grammar-f_expression"><code class="xref docutils literal notranslate"><span class="pre">f_expression</span></code></a> [&quot;=&quot;] [&quot;!&quot; <a class="reference internal" href="#grammar-token-python-grammar-conversion"><code class="xref docutils literal notranslate"><span class="pre">conversion</span></code></a>] [&quot;:&quot; <a class="reference internal" href="#grammar-token-python-grammar-format_spec"><code class="xref docutils literal notranslate"><span class="pre">format_spec</span></code></a>] &quot;}&quot;
  757. <strong id="grammar-token-python-grammar-f_expression">f_expression </strong> ::= (<a class="reference internal" href="expressions.html#grammar-token-python-grammar-conditional_expression"><code class="xref docutils literal notranslate"><span class="pre">conditional_expression</span></code></a> | &quot;*&quot; <a class="reference internal" href="expressions.html#grammar-token-python-grammar-or_expr"><code class="xref docutils literal notranslate"><span class="pre">or_expr</span></code></a>)
  758. (&quot;,&quot; <a class="reference internal" href="expressions.html#grammar-token-python-grammar-conditional_expression"><code class="xref docutils literal notranslate"><span class="pre">conditional_expression</span></code></a> | &quot;,&quot; &quot;*&quot; <a class="reference internal" href="expressions.html#grammar-token-python-grammar-or_expr"><code class="xref docutils literal notranslate"><span class="pre">or_expr</span></code></a>)* [&quot;,&quot;]
  759. | <a class="reference internal" href="expressions.html#grammar-token-python-grammar-yield_expression"><code class="xref docutils literal notranslate"><span class="pre">yield_expression</span></code></a>
  760. <strong id="grammar-token-python-grammar-conversion">conversion </strong> ::= &quot;s&quot; | &quot;r&quot; | &quot;a&quot;
  761. <strong id="grammar-token-python-grammar-format_spec">format_spec </strong> ::= (<a class="reference internal" href="#grammar-token-python-grammar-literal_char"><code class="xref docutils literal notranslate"><span class="pre">literal_char</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-replacement_field"><code class="xref docutils literal notranslate"><span class="pre">replacement_field</span></code></a>)*
  762. <strong id="grammar-token-python-grammar-literal_char">literal_char </strong> ::= &lt;any code point except &quot;{&quot;, &quot;}&quot; or NULL&gt;
  763. </pre>
  764. <p>The parts of the string outside curly braces are treated literally,
  765. except that any doubled curly braces <code class="docutils literal notranslate"><span class="pre">'{{'</span></code> or <code class="docutils literal notranslate"><span class="pre">'}}'</span></code> are replaced
  766. with the corresponding single curly brace. A single opening curly
  767. bracket <code class="docutils literal notranslate"><span class="pre">'{'</span></code> marks a replacement field, which starts with a
  768. Python expression. To display both the expression text and its value after
  769. evaluation, (useful in debugging), an equal sign <code class="docutils literal notranslate"><span class="pre">'='</span></code> may be added after the
  770. expression. A conversion field, introduced by an exclamation point <code class="docutils literal notranslate"><span class="pre">'!'</span></code> may
  771. follow. A format specifier may also be appended, introduced by a colon <code class="docutils literal notranslate"><span class="pre">':'</span></code>.
  772. A replacement field ends with a closing curly bracket <code class="docutils literal notranslate"><span class="pre">'}'</span></code>.</p>
  773. <p>Expressions in formatted string literals are treated like regular
  774. Python expressions surrounded by parentheses, with a few exceptions.
  775. An empty expression is not allowed, and both <a class="reference internal" href="expressions.html#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a> and
  776. assignment expressions <code class="docutils literal notranslate"><span class="pre">:=</span></code> must be surrounded by explicit parentheses.
  777. Each expression is evaluated in the context where the formatted string literal
  778. appears, in order from left to right. Replacement expressions can contain
  779. newlines in both single-quoted and triple-quoted f-strings and they can contain
  780. comments. Everything that comes after a <code class="docutils literal notranslate"><span class="pre">#</span></code> inside a replacement field
  781. is a comment (even closing braces and quotes). In that case, replacement fields
  782. must be closed in a different line.</p>
  783. <div class="highlight-text notranslate"><div class="highlight"><pre><span></span>&gt;&gt;&gt; f&quot;abc{a # This is a comment }&quot;
  784. ... + 3}&quot;
  785. &#39;abc5&#39;
  786. </pre></div>
  787. </div>
  788. <div class="versionchanged">
  789. <p><span class="versionmodified changed">Changed in version 3.7: </span>Prior to Python 3.7, an <a class="reference internal" href="expressions.html#await"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">await</span></code></a> expression and comprehensions
  790. containing an <a class="reference internal" href="compound_stmts.html#async-for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">for</span></code></a> clause were illegal in the expressions
  791. in formatted string literals due to a problem with the implementation.</p>
  792. </div>
  793. <div class="versionchanged">
  794. <p><span class="versionmodified changed">Changed in version 3.12: </span>Prior to Python 3.12, comments were not allowed inside f-string replacement
  795. fields.</p>
  796. </div>
  797. <p>When the equal sign <code class="docutils literal notranslate"><span class="pre">'='</span></code> is provided, the output will have the expression
  798. text, the <code class="docutils literal notranslate"><span class="pre">'='</span></code> and the evaluated value. Spaces after the opening brace
  799. <code class="docutils literal notranslate"><span class="pre">'{'</span></code>, within the expression and after the <code class="docutils literal notranslate"><span class="pre">'='</span></code> are all retained in the
  800. output. By default, the <code class="docutils literal notranslate"><span class="pre">'='</span></code> causes the <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> of the expression to be
  801. provided, unless there is a format specified. When a format is specified it
  802. defaults to the <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> of the expression unless a conversion <code class="docutils literal notranslate"><span class="pre">'!r'</span></code> is
  803. declared.</p>
  804. <div class="versionadded">
  805. <p><span class="versionmodified added">New in version 3.8: </span>The equal sign <code class="docutils literal notranslate"><span class="pre">'='</span></code>.</p>
  806. </div>
  807. <p>If a conversion is specified, the result of evaluating the expression
  808. is converted before formatting. Conversion <code class="docutils literal notranslate"><span class="pre">'!s'</span></code> calls <a class="reference internal" href="../library/stdtypes.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> on
  809. the result, <code class="docutils literal notranslate"><span class="pre">'!r'</span></code> calls <a class="reference internal" href="../library/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> calls <a class="reference internal" href="../library/functions.html#ascii" title="ascii"><code class="xref py py-func docutils literal notranslate"><span class="pre">ascii()</span></code></a>.</p>
  810. <p>The result is then formatted using the <a class="reference internal" href="../library/functions.html#format" title="format"><code class="xref py py-func docutils literal notranslate"><span class="pre">format()</span></code></a> protocol. The
  811. format specifier is passed to the <a class="reference internal" href="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
  812. expression or conversion result. An empty string is passed when the
  813. format specifier is omitted. The formatted result is then included in
  814. the final value of the whole string.</p>
  815. <p>Top-level format specifiers may include nested replacement fields. These nested
  816. fields may include their own conversion fields and <a class="reference internal" href="../library/string.html#formatspec"><span class="std std-ref">format specifiers</span></a>, but may not include more deeply nested replacement fields. The
  817. <a class="reference internal" href="../library/string.html#formatspec"><span class="std std-ref">format specifier mini-language</span></a> is the same as that used by
  818. the <a class="reference internal" href="../library/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.</p>
  819. <p>Formatted string literals may be concatenated, but replacement fields
  820. cannot be split across literals.</p>
  821. <p>Some examples of formatted string literals:</p>
  822. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;Fred&quot;</span>
  823. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;He said his name is </span><span class="si">{</span><span class="n">name</span><span class="si">!r}</span><span class="s2">.&quot;</span>
  824. <span class="go">&quot;He said his name is &#39;Fred&#39;.&quot;</span>
  825. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;He said his name is </span><span class="si">{</span><span class="nb">repr</span><span class="p">(</span><span class="n">name</span><span class="p">)</span><span class="si">}</span><span class="s2">.&quot;</span> <span class="c1"># repr() is equivalent to !r</span>
  826. <span class="go">&quot;He said his name is &#39;Fred&#39;.&quot;</span>
  827. <span class="gp">&gt;&gt;&gt; </span><span class="n">width</span> <span class="o">=</span> <span class="mi">10</span>
  828. <span class="gp">&gt;&gt;&gt; </span><span class="n">precision</span> <span class="o">=</span> <span class="mi">4</span>
  829. <span class="gp">&gt;&gt;&gt; </span><span class="n">value</span> <span class="o">=</span> <span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">(</span><span class="s2">&quot;12.34567&quot;</span><span class="p">)</span>
  830. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;result: </span><span class="si">{</span><span class="n">value</span><span class="si">:{</span><span class="n">width</span><span class="si">}</span><span class="s2">.</span><span class="si">{</span><span class="n">precision</span><span class="si">}}</span><span class="s2">&quot;</span> <span class="c1"># nested fields</span>
  831. <span class="go">&#39;result: 12.35&#39;</span>
  832. <span class="gp">&gt;&gt;&gt; </span><span class="n">today</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="n">year</span><span class="o">=</span><span class="mi">2017</span><span class="p">,</span> <span class="n">month</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">day</span><span class="o">=</span><span class="mi">27</span><span class="p">)</span>
  833. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">today</span><span class="si">:</span><span class="s2">%B %d, %Y</span><span class="si">}</span><span class="s2">&quot;</span> <span class="c1"># using date format specifier</span>
  834. <span class="go">&#39;January 27, 2017&#39;</span>
  835. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">today</span><span class="si">=:</span><span class="s2">%B %d, %Y</span><span class="si">}</span><span class="s2">&quot;</span> <span class="c1"># using date format specifier and debugging</span>
  836. <span class="go">&#39;today=January 27, 2017&#39;</span>
  837. <span class="gp">&gt;&gt;&gt; </span><span class="n">number</span> <span class="o">=</span> <span class="mi">1024</span>
  838. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">number</span><span class="si">:</span><span class="s2">#0x</span><span class="si">}</span><span class="s2">&quot;</span> <span class="c1"># using integer format specifier</span>
  839. <span class="go">&#39;0x400&#39;</span>
  840. <span class="gp">&gt;&gt;&gt; </span><span class="n">foo</span> <span class="o">=</span> <span class="s2">&quot;bar&quot;</span>
  841. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="w"> </span><span class="n">foo</span><span class="w"> </span><span class="si">= }</span><span class="s2">&quot;</span> <span class="c1"># preserves whitespace</span>
  842. <span class="go">&quot; foo = &#39;bar&#39;&quot;</span>
  843. <span class="gp">&gt;&gt;&gt; </span><span class="n">line</span> <span class="o">=</span> <span class="s2">&quot;The mill&#39;s closed&quot;</span>
  844. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">line</span><span class="w"> </span><span class="si">= }</span><span class="s2">&quot;</span>
  845. <span class="go">&#39;line = &quot;The mill\&#39;s closed&quot;&#39;</span>
  846. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">line</span><span class="w"> </span><span class="si">= :</span><span class="s2">20</span><span class="si">}</span><span class="s2">&quot;</span>
  847. <span class="go">&quot;line = The mill&#39;s closed &quot;</span>
  848. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">line</span><span class="w"> </span><span class="si">= !r:</span><span class="s2">20</span><span class="si">}</span><span class="s2">&quot;</span>
  849. <span class="go">&#39;line = &quot;The mill\&#39;s closed&quot; &#39;</span>
  850. </pre></div>
  851. </div>
  852. <p>Reusing the outer f-string quoting type inside a replacement field is
  853. permitted:</p>
  854. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
  855. <span class="gp">&gt;&gt;&gt; </span><span class="sa">f</span><span class="s2">&quot;abc </span><span class="si">{</span><span class="n">a</span><span class="p">[</span><span class="s2">&quot;x&quot;</span><span class="p">]</span><span class="si">}</span><span class="s2"> def&quot;</span>
  856. <span class="go">&#39;abc 2 def&#39;</span>
  857. </pre></div>
  858. </div>
  859. <div class="versionchanged">
  860. <p><span class="versionmodified changed">Changed in version 3.12: </span>Prior to Python 3.12, reuse of the same quoting type of the outer f-string
  861. inside a replacement field was not possible.</p>
  862. </div>
  863. <p>Backslashes are also allowed in replacement fields and are evaluated the same
  864. way as in any other context:</p>
  865. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">,</span> <span class="s2">&quot;c&quot;</span><span class="p">]</span>
  866. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;List a contains:</span><span class="se">\n</span><span class="si">{</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">a</span><span class="p">)</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
  867. <span class="go">List a contains:</span>
  868. <span class="go">a</span>
  869. <span class="go">b</span>
  870. <span class="go">c</span>
  871. </pre></div>
  872. </div>
  873. <div class="versionchanged">
  874. <p><span class="versionmodified changed">Changed in version 3.12: </span>Prior to Python 3.12, backslashes were not permitted inside an f-string
  875. replacement field.</p>
  876. </div>
  877. <p>Formatted string literals cannot be used as docstrings, even if they do not
  878. include expressions.</p>
  879. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span>
  880. <span class="gp">... </span> <span class="sa">f</span><span class="s2">&quot;Not a docstring&quot;</span>
  881. <span class="gp">...</span>
  882. <span class="gp">&gt;&gt;&gt; </span><span class="n">foo</span><span class="o">.</span><span class="vm">__doc__</span> <span class="ow">is</span> <span class="kc">None</span>
  883. <span class="go">True</span>
  884. </pre></div>
  885. </div>
  886. <p>See also <span class="target" id="index-26"></span><a class="pep reference external" href="https://peps.python.org/pep-0498/"><strong>PEP 498</strong></a> for the proposal that added formatted string literals,
  887. and <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.format()</span></code></a>, which uses a related format string mechanism.</p>
  888. </section>
  889. <section id="numeric-literals">
  890. <span id="numbers"></span><h3><span class="section-number">2.4.4. </span>Numeric literals<a class="headerlink" href="#numeric-literals" title="Link to this heading">¶</a></h3>
  891. <p id="index-27">There are three types of numeric literals: integers, floating point numbers, and
  892. imaginary numbers. There are no complex literals (complex numbers can be formed
  893. by adding a real number and an imaginary number).</p>
  894. <p>Note that numeric literals do not include a sign; a phrase like <code class="docutils literal notranslate"><span class="pre">-1</span></code> is
  895. actually an expression composed of the unary operator ‘<code class="docutils literal notranslate"><span class="pre">-</span></code>’ and the literal
  896. <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  897. </section>
  898. <section id="integer-literals">
  899. <span id="integers"></span><span id="index-28"></span><h3><span class="section-number">2.4.5. </span>Integer literals<a class="headerlink" href="#integer-literals" title="Link to this heading">¶</a></h3>
  900. <p>Integer literals are described by the following lexical definitions:</p>
  901. <pre>
  902. <strong id="grammar-token-python-grammar-integer">integer </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-decinteger"><code class="xref docutils literal notranslate"><span class="pre">decinteger</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-bininteger"><code class="xref docutils literal notranslate"><span class="pre">bininteger</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-octinteger"><code class="xref docutils literal notranslate"><span class="pre">octinteger</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-hexinteger"><code class="xref docutils literal notranslate"><span class="pre">hexinteger</span></code></a>
  903. <strong id="grammar-token-python-grammar-decinteger">decinteger </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-nonzerodigit"><code class="xref docutils literal notranslate"><span class="pre">nonzerodigit</span></code></a> ([&quot;_&quot;] <a class="reference internal" href="#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>)* | &quot;0&quot;+ ([&quot;_&quot;] &quot;0&quot;)*
  904. <strong id="grammar-token-python-grammar-bininteger">bininteger </strong> ::= &quot;0&quot; (&quot;b&quot; | &quot;B&quot;) ([&quot;_&quot;] <a class="reference internal" href="#grammar-token-python-grammar-bindigit"><code class="xref docutils literal notranslate"><span class="pre">bindigit</span></code></a>)+
  905. <strong id="grammar-token-python-grammar-octinteger">octinteger </strong> ::= &quot;0&quot; (&quot;o&quot; | &quot;O&quot;) ([&quot;_&quot;] <a class="reference internal" href="#grammar-token-python-grammar-octdigit"><code class="xref docutils literal notranslate"><span class="pre">octdigit</span></code></a>)+
  906. <strong id="grammar-token-python-grammar-hexinteger">hexinteger </strong> ::= &quot;0&quot; (&quot;x&quot; | &quot;X&quot;) ([&quot;_&quot;] <a class="reference internal" href="#grammar-token-python-grammar-hexdigit"><code class="xref docutils literal notranslate"><span class="pre">hexdigit</span></code></a>)+
  907. <strong id="grammar-token-python-grammar-nonzerodigit">nonzerodigit</strong> ::= &quot;1&quot;...&quot;9&quot;
  908. <strong id="grammar-token-python-grammar-digit">digit </strong> ::= &quot;0&quot;...&quot;9&quot;
  909. <strong id="grammar-token-python-grammar-bindigit">bindigit </strong> ::= &quot;0&quot; | &quot;1&quot;
  910. <strong id="grammar-token-python-grammar-octdigit">octdigit </strong> ::= &quot;0&quot;...&quot;7&quot;
  911. <strong id="grammar-token-python-grammar-hexdigit">hexdigit </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a> | &quot;a&quot;...&quot;f&quot; | &quot;A&quot;...&quot;F&quot;
  912. </pre>
  913. <p>There is no limit for the length of integer literals apart from what can be
  914. stored in available memory.</p>
  915. <p>Underscores are ignored for determining the numeric value of the literal. They
  916. can be used to group digits for enhanced readability. One underscore can occur
  917. between digits, and after base specifiers like <code class="docutils literal notranslate"><span class="pre">0x</span></code>.</p>
  918. <p>Note that leading zeros in a non-zero decimal number are not allowed. This is
  919. for disambiguation with C-style octal literals, which Python used before version
  920. 3.0.</p>
  921. <p>Some examples of integer literals:</p>
  922. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">7</span> <span class="mi">2147483647</span> <span class="mo">0o177</span> <span class="mb">0b100110111</span>
  923. <span class="mi">3</span> <span class="mi">79228162514264337593543950336</span> <span class="mo">0o377</span> <span class="mh">0xdeadbeef</span>
  924. <span class="mi">100_000_000_000</span> <span class="mb">0b_1110_0101</span>
  925. </pre></div>
  926. </div>
  927. <div class="versionchanged">
  928. <p><span class="versionmodified changed">Changed in version 3.6: </span>Underscores are now allowed for grouping purposes in literals.</p>
  929. </div>
  930. </section>
  931. <section id="floating-point-literals">
  932. <span id="floating"></span><span id="index-29"></span><h3><span class="section-number">2.4.6. </span>Floating point literals<a class="headerlink" href="#floating-point-literals" title="Link to this heading">¶</a></h3>
  933. <p>Floating point literals are described by the following lexical definitions:</p>
  934. <pre>
  935. <strong id="grammar-token-python-grammar-floatnumber">floatnumber </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-pointfloat"><code class="xref docutils literal notranslate"><span class="pre">pointfloat</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-exponentfloat"><code class="xref docutils literal notranslate"><span class="pre">exponentfloat</span></code></a>
  936. <strong id="grammar-token-python-grammar-pointfloat">pointfloat </strong> ::= [<a class="reference internal" href="#grammar-token-python-grammar-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a>] <a class="reference internal" href="#grammar-token-python-grammar-fraction"><code class="xref docutils literal notranslate"><span class="pre">fraction</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a> &quot;.&quot;
  937. <strong id="grammar-token-python-grammar-exponentfloat">exponentfloat</strong> ::= (<a class="reference internal" href="#grammar-token-python-grammar-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-pointfloat"><code class="xref docutils literal notranslate"><span class="pre">pointfloat</span></code></a>) <a class="reference internal" href="#grammar-token-python-grammar-exponent"><code class="xref docutils literal notranslate"><span class="pre">exponent</span></code></a>
  938. <strong id="grammar-token-python-grammar-digitpart">digitpart </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a> ([&quot;_&quot;] <a class="reference internal" href="#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>)*
  939. <strong id="grammar-token-python-grammar-fraction">fraction </strong> ::= &quot;.&quot; <a class="reference internal" href="#grammar-token-python-grammar-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a>
  940. <strong id="grammar-token-python-grammar-exponent">exponent </strong> ::= (&quot;e&quot; | &quot;E&quot;) [&quot;+&quot; | &quot;-&quot;] <a class="reference internal" href="#grammar-token-python-grammar-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a>
  941. </pre>
  942. <p>Note that the integer and exponent parts are always interpreted using radix 10.
  943. For example, <code class="docutils literal notranslate"><span class="pre">077e010</span></code> is legal, and denotes the same number as <code class="docutils literal notranslate"><span class="pre">77e10</span></code>. The
  944. allowed range of floating point literals is implementation-dependent. As in
  945. integer literals, underscores are supported for digit grouping.</p>
  946. <p>Some examples of floating point literals:</p>
  947. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">3.14</span> <span class="mf">10.</span> <span class="mf">.001</span> <span class="mf">1e100</span> <span class="mf">3.14e-10</span> <span class="mf">0e0</span> <span class="mf">3.14_15_93</span>
  948. </pre></div>
  949. </div>
  950. <div class="versionchanged">
  951. <p><span class="versionmodified changed">Changed in version 3.6: </span>Underscores are now allowed for grouping purposes in literals.</p>
  952. </div>
  953. </section>
  954. <section id="imaginary-literals">
  955. <span id="imaginary"></span><span id="index-30"></span><h3><span class="section-number">2.4.7. </span>Imaginary literals<a class="headerlink" href="#imaginary-literals" title="Link to this heading">¶</a></h3>
  956. <p>Imaginary literals are described by the following lexical definitions:</p>
  957. <pre>
  958. <strong id="grammar-token-python-grammar-imagnumber">imagnumber</strong> ::= (<a class="reference internal" href="#grammar-token-python-grammar-floatnumber"><code class="xref docutils literal notranslate"><span class="pre">floatnumber</span></code></a> | <a class="reference internal" href="#grammar-token-python-grammar-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a>) (&quot;j&quot; | &quot;J&quot;)
  959. </pre>
  960. <p>An imaginary literal yields a complex number with a real part of 0.0. Complex
  961. numbers are represented as a pair of floating point numbers and have the same
  962. restrictions on their range. To create a complex number with a nonzero real
  963. part, add a floating point number to it, e.g., <code class="docutils literal notranslate"><span class="pre">(3+4j)</span></code>. Some examples of
  964. imaginary literals:</p>
  965. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">3.14</span><span class="n">j</span> <span class="mf">10.</span><span class="n">j</span> <span class="mi">10</span><span class="n">j</span> <span class="mf">.001</span><span class="n">j</span> <span class="mf">1e100j</span> <span class="mf">3.14e-10</span><span class="n">j</span> <span class="mf">3.14_15_93</span><span class="n">j</span>
  966. </pre></div>
  967. </div>
  968. </section>
  969. </section>
  970. <section id="operators">
  971. <span id="id14"></span><h2><span class="section-number">2.5. </span>Operators<a class="headerlink" href="#operators" title="Link to this heading">¶</a></h2>
  972. <p id="index-31">The following tokens are operators:</p>
  973. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>+ - * ** / // % @
  974. &lt;&lt; &gt;&gt; &amp; | ^ ~ :=
  975. &lt; &gt; &lt;= &gt;= == !=
  976. </pre></div>
  977. </div>
  978. </section>
  979. <section id="delimiters">
  980. <span id="id15"></span><h2><span class="section-number">2.6. </span>Delimiters<a class="headerlink" href="#delimiters" title="Link to this heading">¶</a></h2>
  981. <p id="index-32">The following tokens serve as delimiters in the grammar:</p>
  982. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>( ) [ ] { }
  983. , : . ; @ = -&gt;
  984. += -= *= /= //= %= @=
  985. &amp;= |= ^= &gt;&gt;= &lt;&lt;= **=
  986. </pre></div>
  987. </div>
  988. <p>The period can also occur in floating-point and imaginary literals. A sequence
  989. of three periods has a special meaning as an ellipsis literal. The second half
  990. of the list, the augmented assignment operators, serve lexically as delimiters,
  991. but also perform an operation.</p>
  992. <p>The following printing ASCII characters have special meaning as part of other
  993. tokens or are otherwise significant to the lexical analyzer:</p>
  994. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>&#39; &quot; # \
  995. </pre></div>
  996. </div>
  997. <p>The following printing ASCII characters are not used in Python. Their
  998. occurrence outside string literals and comments is an unconditional error:</p>
  999. <div class="highlight-none notranslate"><div class="highlight"><pre><span></span>$ ? `
  1000. </pre></div>
  1001. </div>
  1002. <p class="rubric">Footnotes</p>
  1003. <aside class="footnote-list brackets">
  1004. <aside class="footnote brackets" id="id16" role="doc-footnote">
  1005. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id12">1</a><span class="fn-bracket">]</span></span>
  1006. <p><a class="reference external" href="https://www.unicode.org/Public/15.0.0/ucd/NameAliases.txt">https://www.unicode.org/Public/15.0.0/ucd/NameAliases.txt</a></p>
  1007. </aside>
  1008. </aside>
  1009. </section>
  1010. </section>
  1011. <div class="clearer"></div>
  1012. </div>
  1013. </div>
  1014. </div>
  1015. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1016. <div class="sphinxsidebarwrapper">
  1017. <div>
  1018. <h3><a href="../contents.html">Table of Contents</a></h3>
  1019. <ul>
  1020. <li><a class="reference internal" href="#">2. Lexical analysis</a><ul>
  1021. <li><a class="reference internal" href="#line-structure">2.1. Line structure</a><ul>
  1022. <li><a class="reference internal" href="#logical-lines">2.1.1. Logical lines</a></li>
  1023. <li><a class="reference internal" href="#physical-lines">2.1.2. Physical lines</a></li>
  1024. <li><a class="reference internal" href="#comments">2.1.3. Comments</a></li>
  1025. <li><a class="reference internal" href="#encoding-declarations">2.1.4. Encoding declarations</a></li>
  1026. <li><a class="reference internal" href="#explicit-line-joining">2.1.5. Explicit line joining</a></li>
  1027. <li><a class="reference internal" href="#implicit-line-joining">2.1.6. Implicit line joining</a></li>
  1028. <li><a class="reference internal" href="#blank-lines">2.1.7. Blank lines</a></li>
  1029. <li><a class="reference internal" href="#indentation">2.1.8. Indentation</a></li>
  1030. <li><a class="reference internal" href="#whitespace-between-tokens">2.1.9. Whitespace between tokens</a></li>
  1031. </ul>
  1032. </li>
  1033. <li><a class="reference internal" href="#other-tokens">2.2. Other tokens</a></li>
  1034. <li><a class="reference internal" href="#identifiers">2.3. Identifiers and keywords</a><ul>
  1035. <li><a class="reference internal" href="#keywords">2.3.1. Keywords</a></li>
  1036. <li><a class="reference internal" href="#soft-keywords">2.3.2. Soft Keywords</a></li>
  1037. <li><a class="reference internal" href="#reserved-classes-of-identifiers">2.3.3. Reserved classes of identifiers</a></li>
  1038. </ul>
  1039. </li>
  1040. <li><a class="reference internal" href="#literals">2.4. Literals</a><ul>
  1041. <li><a class="reference internal" href="#string-and-bytes-literals">2.4.1. String and Bytes literals</a><ul>
  1042. <li><a class="reference internal" href="#escape-sequences">2.4.1.1. Escape sequences</a></li>
  1043. </ul>
  1044. </li>
  1045. <li><a class="reference internal" href="#string-literal-concatenation">2.4.2. String literal concatenation</a></li>
  1046. <li><a class="reference internal" href="#formatted-string-literals">2.4.3. f-strings</a></li>
  1047. <li><a class="reference internal" href="#numeric-literals">2.4.4. Numeric literals</a></li>
  1048. <li><a class="reference internal" href="#integer-literals">2.4.5. Integer literals</a></li>
  1049. <li><a class="reference internal" href="#floating-point-literals">2.4.6. Floating point literals</a></li>
  1050. <li><a class="reference internal" href="#imaginary-literals">2.4.7. Imaginary literals</a></li>
  1051. </ul>
  1052. </li>
  1053. <li><a class="reference internal" href="#operators">2.5. Operators</a></li>
  1054. <li><a class="reference internal" href="#delimiters">2.6. Delimiters</a></li>
  1055. </ul>
  1056. </li>
  1057. </ul>
  1058. </div>
  1059. <div>
  1060. <h4>Previous topic</h4>
  1061. <p class="topless"><a href="introduction.html"
  1062. title="previous chapter"><span class="section-number">1. </span>Introduction</a></p>
  1063. </div>
  1064. <div>
  1065. <h4>Next topic</h4>
  1066. <p class="topless"><a href="datamodel.html"
  1067. title="next chapter"><span class="section-number">3. </span>Data model</a></p>
  1068. </div>
  1069. <div role="note" aria-label="source link">
  1070. <h3>This Page</h3>
  1071. <ul class="this-page-menu">
  1072. <li><a href="../bugs.html">Report a Bug</a></li>
  1073. <li>
  1074. <a href="https://github.com/python/cpython/blob/main/Doc/reference/lexical_analysis.rst"
  1075. rel="nofollow">Show Source
  1076. </a>
  1077. </li>
  1078. </ul>
  1079. </div>
  1080. </div>
  1081. <div id="sidebarbutton" title="Collapse sidebar">
  1082. <span>«</span>
  1083. </div>
  1084. </div>
  1085. <div class="clearer"></div>
  1086. </div>
  1087. <div class="related" role="navigation" aria-label="related navigation">
  1088. <h3>Navigation</h3>
  1089. <ul>
  1090. <li class="right" style="margin-right: 10px">
  1091. <a href="../genindex.html" title="General Index"
  1092. >index</a></li>
  1093. <li class="right" >
  1094. <a href="../py-modindex.html" title="Python Module Index"
  1095. >modules</a> |</li>
  1096. <li class="right" >
  1097. <a href="datamodel.html" title="3. Data model"
  1098. >next</a> |</li>
  1099. <li class="right" >
  1100. <a href="introduction.html" title="1. Introduction"
  1101. >previous</a> |</li>
  1102. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1103. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1104. <li class="switchers">
  1105. <div class="language_switcher_placeholder"></div>
  1106. <div class="version_switcher_placeholder"></div>
  1107. </li>
  1108. <li>
  1109. </li>
  1110. <li id="cpython-language-and-version">
  1111. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1112. </li>
  1113. <li class="nav-item nav-item-1"><a href="index.html" >The Python Language Reference</a> &#187;</li>
  1114. <li class="nav-item nav-item-this"><a href=""><span class="section-number">2. </span>Lexical analysis</a></li>
  1115. <li class="right">
  1116. <div class="inline-search" role="search">
  1117. <form class="inline-search" action="../search.html" method="get">
  1118. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1119. <input type="submit" value="Go" />
  1120. </form>
  1121. </div>
  1122. |
  1123. </li>
  1124. <li class="right">
  1125. <label class="theme-selector-label">
  1126. Theme
  1127. <select class="theme-selector" oninput="activateTheme(this.value)">
  1128. <option value="auto" selected>Auto</option>
  1129. <option value="light">Light</option>
  1130. <option value="dark">Dark</option>
  1131. </select>
  1132. </label> |</li>
  1133. </ul>
  1134. </div>
  1135. <div class="footer">
  1136. &copy;
  1137. <a href="../copyright.html">
  1138. Copyright
  1139. </a>
  1140. 2001-2024, Python Software Foundation.
  1141. <br />
  1142. This page is licensed under the Python Software Foundation License Version 2.
  1143. <br />
  1144. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1145. <br />
  1146. See <a href="/license.html">History and License</a> for more information.<br />
  1147. <br />
  1148. The Python Software Foundation is a non-profit corporation.
  1149. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1150. <br />
  1151. <br />
  1152. Last updated on Apr 09, 2024 (13:47 UTC).
  1153. <a href="/bugs.html">Found a bug</a>?
  1154. <br />
  1155. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1156. </div>
  1157. </body>
  1158. </html>
上海开阖软件有限公司 沪ICP备12045867号-1