Mini Kabibi Habibi
<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="2. Lexical analysis" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/reference/lexical_analysis.html" />
<meta property="og:site_name" content="Python documentation" />
<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(also known as the tokenizer). This chapter describes how the lexical analyzer brea..." />
<meta property="og:image" content="_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<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(also known as the tokenizer). This chapter describes how the lexical analyzer brea..." />
<meta name="theme-color" content="#3776ab">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<title>2. Lexical analysis — Python 3.14.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=8cd84f99" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />
<script src="../_static/documentation_options.js?v=e4f4b189"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python 3.14.0 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="3. Data model" href="datamodel.html" />
<link rel="prev" title="1. Introduction" href="introduction.html" />
<link rel="canonical" href="https://docs.python.org/3/reference/lexical_analysis.html">
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<link rel="shortcut icon" type="image/png" href="../_static/py.svg">
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
<meta name="readthedocs-addons-api-version" content="1">
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Python logo">
</a>
<span class="version_switcher_placeholder"></span>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<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>
</svg>
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q">
<input type="submit" value="Go">
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<h3><a href="../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">2. Lexical analysis</a><ul>
<li><a class="reference internal" href="#line-structure">2.1. Line structure</a><ul>
<li><a class="reference internal" href="#logical-lines">2.1.1. Logical lines</a></li>
<li><a class="reference internal" href="#physical-lines">2.1.2. Physical lines</a></li>
<li><a class="reference internal" href="#comments">2.1.3. Comments</a></li>
<li><a class="reference internal" href="#encoding-declarations">2.1.4. Encoding declarations</a></li>
<li><a class="reference internal" href="#explicit-line-joining">2.1.5. Explicit line joining</a></li>
<li><a class="reference internal" href="#implicit-line-joining">2.1.6. Implicit line joining</a></li>
<li><a class="reference internal" href="#blank-lines">2.1.7. Blank lines</a></li>
<li><a class="reference internal" href="#indentation">2.1.8. Indentation</a></li>
<li><a class="reference internal" href="#whitespace-between-tokens">2.1.9. Whitespace between tokens</a></li>
<li><a class="reference internal" href="#end-marker">2.1.10. End marker</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-tokens">2.2. Other tokens</a></li>
<li><a class="reference internal" href="#names-identifiers-and-keywords">2.3. Names (identifiers and keywords)</a><ul>
<li><a class="reference internal" href="#keywords">2.3.1. Keywords</a></li>
<li><a class="reference internal" href="#soft-keywords">2.3.2. Soft Keywords</a></li>
<li><a class="reference internal" href="#reserved-classes-of-identifiers">2.3.3. Reserved classes of identifiers</a></li>
</ul>
</li>
<li><a class="reference internal" href="#literals">2.4. Literals</a></li>
<li><a class="reference internal" href="#string-and-bytes-literals">2.5. String and Bytes literals</a><ul>
<li><a class="reference internal" href="#triple-quoted-strings">2.5.1. Triple-quoted strings</a></li>
<li><a class="reference internal" href="#string-prefixes">2.5.2. String prefixes</a></li>
<li><a class="reference internal" href="#formal-grammar">2.5.3. Formal grammar</a></li>
<li><a class="reference internal" href="#escape-sequences">2.5.4. Escape sequences</a><ul>
<li><a class="reference internal" href="#ignored-end-of-line">2.5.4.1. Ignored end of line</a></li>
<li><a class="reference internal" href="#escaped-characters">2.5.4.2. Escaped characters</a></li>
<li><a class="reference internal" href="#octal-character">2.5.4.3. Octal character</a></li>
<li><a class="reference internal" href="#hexadecimal-character">2.5.4.4. Hexadecimal character</a></li>
<li><a class="reference internal" href="#named-unicode-character">2.5.4.5. Named Unicode character</a></li>
<li><a class="reference internal" href="#hexadecimal-unicode-characters">2.5.4.6. Hexadecimal Unicode characters</a></li>
<li><a class="reference internal" href="#unrecognized-escape-sequences">2.5.4.7. Unrecognized escape sequences</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bytes-literals">2.5.5. Bytes literals</a></li>
<li><a class="reference internal" href="#raw-string-literals">2.5.6. Raw string literals</a></li>
<li><a class="reference internal" href="#formatted-string-literals">2.5.7. f-strings</a></li>
<li><a class="reference internal" href="#template-string-literals">2.5.8. t-strings</a></li>
</ul>
</li>
<li><a class="reference internal" href="#numeric-literals">2.6. Numeric literals</a><ul>
<li><a class="reference internal" href="#integer-literals">2.6.1. Integer literals</a></li>
<li><a class="reference internal" href="#floating-point-literals">2.6.2. Floating-point literals</a></li>
<li><a class="reference internal" href="#imaginary-literals">2.6.3. Imaginary literals</a></li>
</ul>
</li>
<li><a class="reference internal" href="#operators-and-delimiters">2.7. Operators and delimiters</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="introduction.html"
title="previous chapter"><span class="section-number">1. </span>Introduction</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="datamodel.html"
title="next chapter"><span class="section-number">3. </span>Data model</a></p>
</div>
<div role="note" aria-label="source link">
<h3>This page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/reference/lexical_analysis.rst?plain=1"
rel="nofollow">Show source
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="datamodel.html" title="3. Data model"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="introduction.html" title="1. Introduction"
accesskey="P">previous</a> |</li>
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.14.0 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Language Reference</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="section-number">2. </span>Lexical analysis</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">
<input type="submit" value="Go">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="lexical-analysis">
<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>
<p id="index-0">A Python program is read by a <em>parser</em>. Input to the parser is a stream of
<a class="reference internal" href="../glossary.html#term-token"><span class="xref std std-term">tokens</span></a>, generated by the <em>lexical analyzer</em> (also known as
the <em>tokenizer</em>).
This chapter describes how the lexical analyzer breaks a file into tokens.</p>
<p>Python reads program text as Unicode code points; the encoding of a source file
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>
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
raised.</p>
<section id="line-structure">
<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>
<p id="index-2">A Python program is divided into a number of <em>logical lines</em>.</p>
<section id="logical-lines">
<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>
<p id="index-3">The end of a logical line is represented by the token <a class="reference internal" href="../library/token.html#token.NEWLINE" title="token.NEWLINE"><code class="xref py py-data docutils literal notranslate"><span class="pre">NEWLINE</span></code></a>.
Statements cannot cross logical line boundaries except where <code class="xref py py-data docutils literal notranslate"><span class="pre">NEWLINE</span></code>
is allowed by the syntax (e.g., between statements in compound statements).
A logical line is constructed from one or more <em>physical lines</em> by following
the <a class="reference internal" href="#explicit-joining"><span class="std std-ref">explicit</span></a> or <a class="reference internal" href="#implicit-joining"><span class="std std-ref">implicit</span></a>
<em>line joining</em> rules.</p>
</section>
<section id="physical-lines">
<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>
<p>A physical line is a sequence of characters terminated by one the following
end-of-line sequences:</p>
<ul class="simple">
<li><p>the Unix form using ASCII LF (linefeed),</p></li>
<li><p>the Windows form using the ASCII sequence CR LF (return followed by linefeed),</p></li>
<li><p>the ‘<a class="reference external" href="https://en.wikipedia.org/wiki/Classic_Mac_OS">Classic Mac OS</a>’ form using the ASCII CR (return) character.</p>
</li>
</ul>
<p>Regardless of platform, each of these sequences is replaced by a single
ASCII LF (linefeed) character.
(This is done even inside <a class="reference internal" href="#strings"><span class="std std-ref">string literals</span></a>.)
Each line can use any of the sequences; they do not need to be consistent
within a file.</p>
<p>The end of input also serves as an implicit terminator for the final
physical line.</p>
<p>Formally:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-newline">newline</strong>: <ASCII LF> | <ASCII CR> <ASCII LF> | <ASCII CR>
</pre>
</section>
<section id="comments">
<span id="id5"></span><h3><span class="section-number">2.1.3. </span>Comments<a class="headerlink" href="#comments" title="Link to this heading">¶</a></h3>
<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
literal, and ends at the end of the physical line. A comment signifies the end
of the logical line unless the implicit line joining rules are invoked. Comments
are ignored by the syntax.</p>
</section>
<section id="encoding-declarations">
<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>
<p id="index-5">If a comment in the first or second line of the Python script matches the
regular expression <code class="docutils literal notranslate"><span class="pre">coding[=:]\s*([-\w.]+)</span></code>, this comment is processed as an
encoding declaration; the first group of this expression names the encoding of
the source code file. The encoding declaration must appear on a line of its
own. If it is the second line, the first line must also be a comment-only line.
The recommended forms of an encoding expression are</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># -*- coding: <encoding-name> -*-</span>
</pre></div>
</div>
<p>which is recognized also by GNU Emacs, and</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># vim:fileencoding=<encoding-name></span>
</pre></div>
</div>
<p>which is recognized by Bram Moolenaar’s VIM.</p>
<p>If no encoding declaration is found, the default encoding is UTF-8. If the
implicit or explicit encoding of a file is UTF-8, an initial UTF-8 byte-order
mark (<code class="docutils literal notranslate"><span class="pre">b'\xef\xbb\xbf'</span></code>) is ignored rather than being a syntax error.</p>
<p>If an encoding is declared, the encoding name must be recognized by Python
(see <a class="reference internal" href="../library/codecs.html#standard-encodings"><span class="std std-ref">Standard Encodings</span></a>). The
encoding is used for all lexical analysis, including string literals, comments
and identifiers.</p>
<p>All lexical analysis, including string literals, comments
and identifiers, works on Unicode text decoded using the source encoding.
Any Unicode code point, except the NUL control character, can appear in
Python source.</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-source_character">source_character</strong>: <any Unicode code point, except NUL>
</pre>
</section>
<section id="explicit-line-joining">
<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>
<p id="index-6">Two or more physical lines may be joined into logical lines using backslash
characters (<code class="docutils literal notranslate"><span class="pre">\</span></code>), as follows: when a physical line ends in a backslash that is
not part of a string literal or comment, it is joined with the following forming
a single logical line, deleting the backslash and the following end-of-line
character. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="mi">1900</span> <span class="o"><</span> <span class="n">year</span> <span class="o"><</span> <span class="mi">2100</span> <span class="ow">and</span> <span class="mi">1</span> <span class="o"><=</span> <span class="n">month</span> <span class="o"><=</span> <span class="mi">12</span> \
<span class="ow">and</span> <span class="mi">1</span> <span class="o"><=</span> <span class="n">day</span> <span class="o"><=</span> <span class="mi">31</span> <span class="ow">and</span> <span class="mi">0</span> <span class="o"><=</span> <span class="n">hour</span> <span class="o"><</span> <span class="mi">24</span> \
<span class="ow">and</span> <span class="mi">0</span> <span class="o"><=</span> <span class="n">minute</span> <span class="o"><</span> <span class="mi">60</span> <span class="ow">and</span> <span class="mi">0</span> <span class="o"><=</span> <span class="n">second</span> <span class="o"><</span> <span class="mi">60</span><span class="p">:</span> <span class="c1"># Looks like a valid date</span>
<span class="k">return</span> <span class="mi">1</span>
</pre></div>
</div>
<p>A line ending in a backslash cannot carry a comment. A backslash does not
continue a comment. A backslash does not continue a token except for string
literals (i.e., tokens other than string literals cannot be split across
physical lines using a backslash). A backslash is illegal elsewhere on a line
outside a string literal.</p>
</section>
<section id="implicit-line-joining">
<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>
<p>Expressions in parentheses, square brackets or curly braces can be split over
more than one physical line without using backslashes. For example:</p>
<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">'Januari'</span><span class="p">,</span> <span class="s1">'Februari'</span><span class="p">,</span> <span class="s1">'Maart'</span><span class="p">,</span> <span class="c1"># These are the</span>
<span class="s1">'April'</span><span class="p">,</span> <span class="s1">'Mei'</span><span class="p">,</span> <span class="s1">'Juni'</span><span class="p">,</span> <span class="c1"># Dutch names</span>
<span class="s1">'Juli'</span><span class="p">,</span> <span class="s1">'Augustus'</span><span class="p">,</span> <span class="s1">'September'</span><span class="p">,</span> <span class="c1"># for the months</span>
<span class="s1">'Oktober'</span><span class="p">,</span> <span class="s1">'November'</span><span class="p">,</span> <span class="s1">'December'</span><span class="p">]</span> <span class="c1"># of the year</span>
</pre></div>
</div>
<p>Implicitly continued lines can carry comments. The indentation of the
continuation lines is not important. Blank continuation lines are allowed.
There is no NEWLINE token between implicit continuation lines. Implicitly
continued lines can also occur within triple-quoted strings (see below); in that
case they cannot carry comments.</p>
</section>
<section id="blank-lines">
<span id="id6"></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>
<p id="index-7">A logical line that contains only spaces, tabs, formfeeds and possibly a
comment, is ignored (i.e., no <a class="reference internal" href="../library/token.html#token.NEWLINE" title="token.NEWLINE"><code class="xref py py-data docutils literal notranslate"><span class="pre">NEWLINE</span></code></a> token is generated).
During interactive input of statements, handling of a blank line may differ
depending on the implementation of the read-eval-print loop.
In the standard interactive interpreter, an entirely blank logical line (that
is, one containing not even whitespace or a comment) terminates a multi-line
statement.</p>
</section>
<section id="indentation">
<span id="id7"></span><h3><span class="section-number">2.1.8. </span>Indentation<a class="headerlink" href="#indentation" title="Link to this heading">¶</a></h3>
<p id="index-8">Leading whitespace (spaces and tabs) at the beginning of a logical line is used
to compute the indentation level of the line, which in turn is used to determine
the grouping of statements.</p>
<p>Tabs are replaced (from left to right) by one to eight spaces such that the
total number of characters up to and including the replacement is a multiple of
eight (this is intended to be the same rule as used by Unix). The total number
of spaces preceding the first non-blank character then determines the line’s
indentation. Indentation cannot be split over multiple physical lines using
backslashes; the whitespace up to the first backslash determines the
indentation.</p>
<p>Indentation is rejected as inconsistent if a source file mixes tabs and spaces
in a way that makes the meaning dependent on the worth of a tab in spaces; a
<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>
<p><strong>Cross-platform compatibility note:</strong> because of the nature of text editors on
non-UNIX platforms, it is unwise to use a mixture of spaces and tabs for the
indentation in a single source file. It should also be noted that different
platforms may explicitly limit the maximum indentation level.</p>
<p>A formfeed character may be present at the start of the line; it will be ignored
for the indentation calculations above. Formfeed characters occurring elsewhere
in the leading whitespace have an undefined effect (for instance, they may reset
the space count to zero).</p>
<p id="index-9">The indentation levels of consecutive lines are used to generate
<a class="reference internal" href="../library/token.html#token.INDENT" title="token.INDENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">INDENT</span></code></a> and <a class="reference internal" href="../library/token.html#token.DEDENT" title="token.DEDENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEDENT</span></code></a> tokens, using a stack,
as follows.</p>
<p>Before the first line of the file is read, a single zero is pushed on the stack;
this will never be popped off again. The numbers pushed on the stack will
always be strictly increasing from bottom to top. At the beginning of each
logical line, the line’s indentation level is compared to the top of the stack.
If it is equal, nothing happens. If it is larger, it is pushed on the stack, and
one <code class="xref py py-data docutils literal notranslate"><span class="pre">INDENT</span></code> token is generated. If it is smaller, it <em>must</em> be one of the
numbers occurring on the stack; all numbers on the stack that are larger are
popped off, and for each number popped off a <code class="xref py py-data docutils literal notranslate"><span class="pre">DEDENT</span></code> token is generated.
At the end of the file, a <code class="xref py py-data docutils literal notranslate"><span class="pre">DEDENT</span></code> token is generated for each number
remaining on the stack that is larger than zero.</p>
<p>Here is an example of a correctly (though confusingly) indented piece of Python
code:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">perm</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
<span class="c1"># Compute the list of all permutations of l</span>
<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"><=</span> <span class="mi">1</span><span class="p">:</span>
<span class="k">return</span> <span class="p">[</span><span class="n">l</span><span class="p">]</span>
<span class="n">r</span> <span class="o">=</span> <span class="p">[]</span>
<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="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>
<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>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span>
<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>
<span class="k">return</span> <span class="n">r</span>
</pre></div>
</div>
<p>The following example shows various indentation errors:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span> <span class="k">def</span><span class="w"> </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>
<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>
<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>
<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>
<span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span>
<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>
<span class="k">return</span> <span class="n">r</span> <span class="c1"># error: inconsistent dedent</span>
</pre></div>
</div>
<p>(Actually, the first three errors are detected by the parser; only the last
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
not match a level popped off the stack.)</p>
</section>
<section id="whitespace-between-tokens">
<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>
<p>Except at the beginning of a logical line or in string literals, the whitespace
characters space, tab and formfeed can be used interchangeably to separate
tokens. Whitespace is needed between two tokens only if their concatenation
could otherwise be interpreted as a different token. For example, <code class="docutils literal notranslate"><span class="pre">ab</span></code> is one
token, but <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">b</span></code> is two tokens. However, <code class="docutils literal notranslate"><span class="pre">+a</span></code> and <code class="docutils literal notranslate"><span class="pre">+</span> <span class="pre">a</span></code> both produce
two tokens, <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">a</span></code>, as <code class="docutils literal notranslate"><span class="pre">+a</span></code> is not a valid token.</p>
</section>
<section id="end-marker">
<span id="endmarker-token"></span><h3><span class="section-number">2.1.10. </span>End marker<a class="headerlink" href="#end-marker" title="Link to this heading">¶</a></h3>
<p>At the end of non-interactive input, the lexical analyzer generates an
<a class="reference internal" href="../library/token.html#token.ENDMARKER" title="token.ENDMARKER"><code class="xref py py-data docutils literal notranslate"><span class="pre">ENDMARKER</span></code></a> token.</p>
</section>
</section>
<section id="other-tokens">
<span id="id8"></span><h2><span class="section-number">2.2. </span>Other tokens<a class="headerlink" href="#other-tokens" title="Link to this heading">¶</a></h2>
<p>Besides <a class="reference internal" href="../library/token.html#token.NEWLINE" title="token.NEWLINE"><code class="xref py py-data docutils literal notranslate"><span class="pre">NEWLINE</span></code></a>, <a class="reference internal" href="../library/token.html#token.INDENT" title="token.INDENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">INDENT</span></code></a> and <a class="reference internal" href="../library/token.html#token.DEDENT" title="token.DEDENT"><code class="xref py py-data docutils literal notranslate"><span class="pre">DEDENT</span></code></a>,
the following categories of tokens exist:
<em>identifiers</em> and <em>keywords</em> (<a class="reference internal" href="../library/token.html#token.NAME" title="token.NAME"><code class="xref py py-data docutils literal notranslate"><span class="pre">NAME</span></code></a>), <em>literals</em> (such as
<a class="reference internal" href="../library/token.html#token.NUMBER" title="token.NUMBER"><code class="xref py py-data docutils literal notranslate"><span class="pre">NUMBER</span></code></a> and <a class="reference internal" href="../library/token.html#token.STRING" title="token.STRING"><code class="xref py py-data docutils literal notranslate"><span class="pre">STRING</span></code></a>), and other symbols
(<em>operators</em> and <em>delimiters</em>, <a class="reference internal" href="../library/token.html#token.OP" title="token.OP"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP</span></code></a>).
Whitespace characters (other than logical line terminators, discussed earlier)
are not tokens, but serve to delimit tokens.
Where ambiguity exists, a token comprises the longest possible string that
forms a legal token, when read from left to right.</p>
</section>
<section id="names-identifiers-and-keywords">
<span id="identifiers"></span><h2><span class="section-number">2.3. </span>Names (identifiers and keywords)<a class="headerlink" href="#names-identifiers-and-keywords" title="Link to this heading">¶</a></h2>
<p id="index-10"><a class="reference internal" href="../library/token.html#token.NAME" title="token.NAME"><code class="xref py py-data docutils literal notranslate"><span class="pre">NAME</span></code></a> tokens represent <em>identifiers</em>, <em>keywords</em>, and
<em>soft keywords</em>.</p>
<p>Within the ASCII range (U+0001..U+007F), the valid characters for names
include the uppercase and lowercase letters (<code class="docutils literal notranslate"><span class="pre">A-Z</span></code> and <code class="docutils literal notranslate"><span class="pre">a-z</span></code>),
the underscore <code class="docutils literal notranslate"><span class="pre">_</span></code> and, except for the first character, the digits
<code class="docutils literal notranslate"><span class="pre">0</span></code> through <code class="docutils literal notranslate"><span class="pre">9</span></code>.</p>
<p>Names must contain at least one character, but have no upper length limit.
Case is significant.</p>
<p>Besides <code class="docutils literal notranslate"><span class="pre">A-Z</span></code>, <code class="docutils literal notranslate"><span class="pre">a-z</span></code>, <code class="docutils literal notranslate"><span class="pre">_</span></code> and <code class="docutils literal notranslate"><span class="pre">0-9</span></code>, names can also use “letter-like”
and “number-like” characters from outside the ASCII range, as detailed below.</p>
<p>All identifiers are converted into the <a class="reference external" href="https://www.unicode.org/reports/tr15/#Norm_Forms">normalization form</a> NFKC while
parsing; comparison of identifiers is based on NFKC.</p>
<p>Formally, the first character of a normalized identifier must belong to the
set <code class="docutils literal notranslate"><span class="pre">id_start</span></code>, which is the union of:</p>
<ul class="simple">
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Lu></span></code> - uppercase letters (includes <code class="docutils literal notranslate"><span class="pre">A</span></code> to <code class="docutils literal notranslate"><span class="pre">Z</span></code>)</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Ll></span></code> - lowercase letters (includes <code class="docutils literal notranslate"><span class="pre">a</span></code> to <code class="docutils literal notranslate"><span class="pre">z</span></code>)</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Lt></span></code> - titlecase letters</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Lm></span></code> - modifier letters</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Lo></span></code> - other letters</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Nl></span></code> - letter numbers</p></li>
<li><p>{<code class="docutils literal notranslate"><span class="pre">"_"</span></code>} - the underscore</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre"><Other_ID_Start></span></code> - an explicit set of characters in <a class="reference external" href="https://www.unicode.org/Public/16.0.0/ucd/PropList.txt">PropList.txt</a>
to support backwards compatibility</p></li>
</ul>
<p>The remaining characters must belong to the set <code class="docutils literal notranslate"><span class="pre">id_continue</span></code>, which is the
union of:</p>
<ul class="simple">
<li><p>all characters in <code class="docutils literal notranslate"><span class="pre">id_start</span></code></p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Nd></span></code> - decimal numbers (includes <code class="docutils literal notranslate"><span class="pre">0</span></code> to <code class="docutils literal notranslate"><span class="pre">9</span></code>)</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Pc></span></code> - connector punctuations</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Mn></span></code> - nonspacing marks</p></li>
<li><p>Unicode category <code class="docutils literal notranslate"><span class="pre"><Mc></span></code> - spacing combining marks</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre"><Other_ID_Continue></span></code> - another explicit set of characters in
<a class="reference external" href="https://www.unicode.org/Public/16.0.0/ucd/PropList.txt">PropList.txt</a> to support backwards compatibility</p></li>
</ul>
<p>Unicode categories use the version of the 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>
<p>These sets are based on the Unicode standard annex <a class="reference external" href="https://www.unicode.org/reports/tr31/">UAX-31</a>.
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 further details.</p>
<p>Even more formally, names are described by the following lexical definitions:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-NAME">NAME</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>*
<strong id="grammar-token-python-grammar-id_start">id_start</strong>: <Lu> | <Ll> | <Lt> | <Lm> | <Lo> | <Nl> | <span class="sx">"_"</span> | <Other_ID_Start>
<strong id="grammar-token-python-grammar-id_continue">id_continue</strong>: <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> | <Nd> | <Pc> | <Mn> | <Mc> | <Other_ID_Continue>
<strong id="grammar-token-python-grammar-xid_start">xid_start</strong>: <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 (<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> <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>*)">
<strong id="grammar-token-python-grammar-xid_continue">xid_continue</strong>: <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 (<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>*)">
<strong id="grammar-token-python-grammar-identifier">identifier</strong>: <<a class="reference internal" href="#grammar-token-python-grammar-NAME"><code class="xref docutils literal notranslate"><span class="pre">NAME</span></code></a>, except keywords>
</pre>
<p>A non-normative listing of all valid identifier characters as defined by
Unicode is available in the <a class="reference external" href="https://www.unicode.org/Public/16.0.0/ucd/DerivedCoreProperties.txt">DerivedCoreProperties.txt</a> file in the Unicode
Character Database.</p>
<section id="keywords">
<span id="id9"></span><h3><span class="section-number">2.3.1. </span>Keywords<a class="headerlink" href="#keywords" title="Link to this heading">¶</a></h3>
<p id="index-12">The following names are used as reserved words, or <em>keywords</em> of the
language, and cannot be used as ordinary identifiers. They must be spelled
exactly as written here:</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>False await else import pass
None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield
</pre></div>
</div>
</section>
<section id="soft-keywords">
<span id="id10"></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>
<div class="versionadded" id="index-13">
<p><span class="versionmodified added">Added in version 3.10.</span></p>
</div>
<p>Some names are only reserved under specific contexts. These are known as
<em>soft keywords</em>:</p>
<ul class="simple">
<li><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>, when 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.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>, when 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></li>
</ul>
<p>These syntactically act as keywords in their specific contexts,
but this distinction is done at the parser level, not when tokenizing.</p>
<p>As soft keywords, their use in the grammar is possible while still
preserving compatibility with existing code that uses these names as
identifier names.</p>
<div class="versionchanged">
<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>
</div>
</section>
<section id="reserved-classes-of-identifiers">
<span id="id-classes"></span><span id="index-14"></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>
<p>Certain classes of identifiers (besides keywords) have special meanings. These
classes are identified by the patterns of leading and trailing underscore
characters:</p>
<dl>
<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>
</dd>
<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
<a class="reference internal" href="#soft-keywords"><span class="std std-ref">soft keyword</span></a> that denotes a
<a class="reference internal" href="compound_stmts.html#wildcard-patterns"><span class="std std-ref">wildcard</span></a>.</p>
<p>Separately, the interactive interpreter makes the result of the last evaluation
available in the variable <code class="docutils literal notranslate"><span class="pre">_</span></code>.
(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
functions like <code class="docutils literal notranslate"><span class="pre">print</span></code>.)</p>
<p>Elsewhere, <code class="docutils literal notranslate"><span class="pre">_</span></code> is a regular identifier. It is often used to name
“special” items, but it is not special to Python itself.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The name <code class="docutils literal notranslate"><span class="pre">_</span></code> is often used in conjunction with internationalization;
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
information on this convention.</p>
<p>It is also commonly used for unused variables.</p>
</div>
</dd>
<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
defined by the interpreter and its implementation (including the standard library).
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.
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,
in any context, that does not follow explicitly documented use, is subject to
breakage without warning.</p>
</dd>
<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
class definition, are re-written to use a mangled form to help avoid name
clashes between “private” attributes of base and derived classes. See section
<a class="reference internal" href="expressions.html#atom-identifiers"><span class="std std-ref">Identifiers (Names)</span></a>.</p>
</dd>
</dl>
</section>
</section>
<section id="literals">
<span id="id11"></span><h2><span class="section-number">2.4. </span>Literals<a class="headerlink" href="#literals" title="Link to this heading">¶</a></h2>
<p id="index-15">Literals are notations for constant values of some built-in types.</p>
<p>In terms of lexical analysis, Python has <a class="reference internal" href="#strings"><span class="std std-ref">string, bytes</span></a>
and <a class="reference internal" href="#numbers"><span class="std std-ref">numeric</span></a> literals.</p>
<p>Other “literals” are lexically denoted using <a class="reference internal" href="#keywords"><span class="std std-ref">keywords</span></a>
(<code class="docutils literal notranslate"><span class="pre">None</span></code>, <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code>) and the special
<a class="reference internal" href="#lexical-ellipsis"><span class="std std-ref">ellipsis token</span></a> (<code class="docutils literal notranslate"><span class="pre">...</span></code>).</p>
</section>
<section id="string-and-bytes-literals">
<span id="strings"></span><span id="index-16"></span><h2><span class="section-number">2.5. </span>String and Bytes literals<a class="headerlink" href="#string-and-bytes-literals" title="Link to this heading">¶</a></h2>
<p>String literals are text enclosed in single quotes (<code class="docutils literal notranslate"><span class="pre">'</span></code>) or double
quotes (<code class="docutils literal notranslate"><span class="pre">"</span></code>). For example:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s2">"spam"</span>
<span class="s1">'eggs'</span>
</pre></div>
</div>
<p>The quote used to start the literal also terminates it, so a string literal
can only contain the other quote (except with escape sequences, see below).
For example:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="s1">'Say "Hello", please.'</span>
<span class="s2">"Don't do that!"</span>
</pre></div>
</div>
<p>Except for this limitation, the choice of quote character (<code class="docutils literal notranslate"><span class="pre">'</span></code> or <code class="docutils literal notranslate"><span class="pre">"</span></code>)
does not affect how the literal is parsed.</p>
<p>Inside a string literal, the backslash (<code class="docutils literal notranslate"><span class="pre">\</span></code>) character introduces an
<em class="dfn">escape sequence</em>, which has special meaning depending on the character
after the backslash.
For example, <code class="docutils literal notranslate"><span class="pre">\"</span></code> denotes the double quote character, and does <em>not</em> end
the string:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="s2">"Say </span><span class="se">\"</span><span class="s2">Hello</span><span class="se">\"</span><span class="s2"> to everyone!"</span><span class="p">)</span>
<span class="go">Say "Hello" to everyone!</span>
</pre></div>
</div>
<p>See <a class="reference internal" href="#escape-sequences"><span class="std std-ref">escape sequences</span></a> below for a full list of such
sequences, and more details.</p>
<section id="triple-quoted-strings">
<span id="index-17"></span><h3><span class="section-number">2.5.1. </span>Triple-quoted strings<a class="headerlink" href="#triple-quoted-strings" title="Link to this heading">¶</a></h3>
<p>Strings can also be enclosed in matching groups of three single or double
quotes.
These are generally referred to as <em class="dfn">triple-quoted strings</em>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""This is a triple-quoted string."""</span>
</pre></div>
</div>
<p>In triple-quoted literals, unescaped quotes are allowed (and are
retained), except that three unescaped quotes in a row terminate the literal,
if they are of the same kind (<code class="docutils literal notranslate"><span class="pre">'</span></code> or <code class="docutils literal notranslate"><span class="pre">"</span></code>) used at the start:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">"""This string has "quotes" inside."""</span>
</pre></div>
</div>
<p>Unescaped newlines are also allowed and retained:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="sd">'''This triple-quoted string</span>
<span class="sd">continues on the next line.'''</span>
</pre></div>
</div>
</section>
<section id="string-prefixes">
<span id="index-18"></span><h3><span class="section-number">2.5.2. </span>String prefixes<a class="headerlink" href="#string-prefixes" title="Link to this heading">¶</a></h3>
<p>String literals can have an optional <em class="dfn">prefix</em> that influences how the
content of the literal is parsed, for example:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="sa">b</span><span class="s2">"data"</span>
<span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">result</span><span class="si">=}</span><span class="s1">'</span>
</pre></div>
</div>
<p>The allowed prefixes are:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">b</span></code>: <a class="reference internal" href="#bytes-literal"><span class="std std-ref">Bytes literal</span></a></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">r</span></code>: <a class="reference internal" href="#raw-strings"><span class="std std-ref">Raw string</span></a></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">f</span></code>: <a class="reference internal" href="#f-strings"><span class="std std-ref">Formatted string literal</span></a> (“f-string”)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">t</span></code>: <a class="reference internal" href="#t-strings"><span class="std std-ref">Template string literal</span></a> (“t-string”)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">u</span></code>: No effect (allowed for backwards compatibility)</p></li>
</ul>
<p>See the linked sections for details on each type.</p>
<p>Prefixes are case-insensitive (for example, ‘<code class="docutils literal notranslate"><span class="pre">B</span></code>’ works the same as ‘<code class="docutils literal notranslate"><span class="pre">b</span></code>’).
The ‘<code class="docutils literal notranslate"><span class="pre">r</span></code>’ prefix can be combined with ‘<code class="docutils literal notranslate"><span class="pre">f</span></code>’, ‘<code class="docutils literal notranslate"><span class="pre">t</span></code>’ or ‘<code class="docutils literal notranslate"><span class="pre">b</span></code>’, so ‘<code class="docutils literal notranslate"><span class="pre">fr</span></code>’,
‘<code class="docutils literal notranslate"><span class="pre">rf</span></code>’, ‘<code class="docutils literal notranslate"><span class="pre">tr</span></code>’, ‘<code class="docutils literal notranslate"><span class="pre">rt</span></code>’, ‘<code class="docutils literal notranslate"><span class="pre">br</span></code>’, and ‘<code class="docutils literal notranslate"><span class="pre">rb</span></code>’ are also valid prefixes.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added 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
of <code class="docutils literal notranslate"><span class="pre">'br'</span></code>.</p>
<p>Support for the unicode legacy literal (<code class="docutils literal notranslate"><span class="pre">u'value'</span></code>) was reintroduced
to simplify the maintenance of dual Python 2.x and 3.x codebases.
See <span class="target" id="index-19"></span><a class="pep reference external" href="https://peps.python.org/pep-0414/"><strong>PEP 414</strong></a> for more information.</p>
</div>
</section>
<section id="formal-grammar">
<h3><span class="section-number">2.5.3. </span>Formal grammar<a class="headerlink" href="#formal-grammar" title="Link to this heading">¶</a></h3>
<p>String literals, except <a class="reference internal" href="#f-strings"><span class="std std-ref">“f-strings”</span></a> and
<a class="reference internal" href="#t-strings"><span class="std std-ref">“t-strings”</span></a>, are described by the
following lexical definitions.</p>
<p>These definitions use <a class="reference internal" href="introduction.html#lexical-lookaheads"><span class="std std-ref">negative lookaheads</span></a> (<code class="docutils literal notranslate"><span class="pre">!</span></code>)
to indicate that an ending quote ends the literal.</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-STRING">STRING</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-stringcontent"><code class="xref docutils literal notranslate"><span class="pre">stringcontent</span></code></a>)
<strong id="grammar-token-python-grammar-stringprefix">stringprefix</strong>: <(<span class="sx">"r"</span> | <span class="sx">"u"</span> | <span class="sx">"b"</span> | <span class="sx">"br"</span> | <span class="sx">"rb"</span>), case-insensitive>
<strong id="grammar-token-python-grammar-stringcontent">stringcontent</strong>:
| <span class="sx">"'''"</span> ( !<span class="sx">"'''"</span> <a class="reference internal" href="#grammar-token-python-grammar-longstringitem"><code class="xref docutils literal notranslate"><span class="pre">longstringitem</span></code></a>)* <span class="sx">"'''"</span>
| <span class="sx">'"""'</span> ( !<span class="sx">'"""'</span> <a class="reference internal" href="#grammar-token-python-grammar-longstringitem"><code class="xref docutils literal notranslate"><span class="pre">longstringitem</span></code></a>)* <span class="sx">'"""'</span>
| <span class="sx">"'"</span> ( !<span class="sx">"'"</span> <a class="reference internal" href="#grammar-token-python-grammar-stringitem"><code class="xref docutils literal notranslate"><span class="pre">stringitem</span></code></a>)* <span class="sx">"'"</span>
| <span class="sx">'"'</span> ( !<span class="sx">'"'</span> <a class="reference internal" href="#grammar-token-python-grammar-stringitem"><code class="xref docutils literal notranslate"><span class="pre">stringitem</span></code></a>)* <span class="sx">'"'</span>
<strong id="grammar-token-python-grammar-stringitem">stringitem</strong>: <a class="reference internal" href="#grammar-token-python-grammar-stringchar"><code class="xref docutils literal notranslate"><span class="pre">stringchar</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>
<strong id="grammar-token-python-grammar-stringchar">stringchar</strong>: <any <a class="reference internal" href="#grammar-token-python-grammar-source_character"><code class="xref docutils literal notranslate"><span class="pre">source_character</span></code></a>, except backslash and newline>
<strong id="grammar-token-python-grammar-longstringitem">longstringitem</strong>: <a class="reference internal" href="#grammar-token-python-grammar-stringitem"><code class="xref docutils literal notranslate"><span class="pre">stringitem</span></code></a> | newline
<strong id="grammar-token-python-grammar-stringescapeseq">stringescapeseq</strong>: <span class="sx">"\"</span> <any <a class="reference internal" href="#grammar-token-python-grammar-source_character"><code class="xref docutils literal notranslate"><span class="pre">source_character</span></code></a>>
</pre>
<p>Note that as in all lexical definitions, whitespace is significant.
In particular, the prefix (if any) must be immediately followed by the starting
quote.</p>
</section>
<section id="escape-sequences">
<span id="index-20"></span><span id="id12"></span><h3><span class="section-number">2.5.4. </span>Escape sequences<a class="headerlink" href="#escape-sequences" title="Link to this heading">¶</a></h3>
<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
bytes literals are interpreted according to rules similar to those used by
Standard C. The recognized escape sequences are:</p>
<table class="docutils align-default">
<thead>
<tr class="row-odd"><th class="head"><p>Escape Sequence</p></th>
<th class="head"><p>Meaning</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\</span></code><newline></p></td>
<td><p><a class="reference internal" href="#string-escape-ignore"><span class="std std-ref">Ignored end of line</span></a></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\\</span></code></p></td>
<td><p><a class="reference internal" href="#string-escape-escaped-char"><span class="std std-ref">Backslash</span></a></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\'</span></code></p></td>
<td><p><a class="reference internal" href="#string-escape-escaped-char"><span class="std std-ref">Single quote</span></a></p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\"</span></code></p></td>
<td><p><a class="reference internal" href="#string-escape-escaped-char"><span class="std std-ref">Double quote</span></a></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\a</span></code></p></td>
<td><p>ASCII Bell (BEL)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\b</span></code></p></td>
<td><p>ASCII Backspace (BS)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\f</span></code></p></td>
<td><p>ASCII Formfeed (FF)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\n</span></code></p></td>
<td><p>ASCII Linefeed (LF)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\r</span></code></p></td>
<td><p>ASCII Carriage Return (CR)</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">\t</span></code></p></td>
<td><p>ASCII Horizontal Tab (TAB)</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">\v</span></code></p></td>
<td><p>ASCII Vertical Tab (VT)</p></td>
</tr>
<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>
<td><p><a class="reference internal" href="#string-escape-oct"><span class="std std-ref">Octal character</span></a></p></td>
</tr>
<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>
<td><p><a class="reference internal" href="#string-escape-hex"><span class="std std-ref">Hexadecimal character</span></a></p></td>
</tr>
<tr class="row-odd"><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>
<td><p><a class="reference internal" href="#string-escape-named"><span class="std std-ref">Named Unicode character</span></a></p></td>
</tr>
<tr class="row-even"><td><p><code class="samp docutils literal notranslate"><span class="pre">\u</span><em><span class="pre">xxxx</span></em></code></p></td>
<td><p><a class="reference internal" href="#string-escape-long-hex"><span class="std std-ref">Hexadecimal Unicode character</span></a></p></td>
</tr>
<tr class="row-odd"><td><p><code class="samp docutils literal notranslate"><span class="pre">\U</span><em><span class="pre">xxxxxxxx</span></em></code></p></td>
<td><p><a class="reference internal" href="#string-escape-long-hex"><span class="std std-ref">Hexadecimal Unicode character</span></a></p></td>
</tr>
</tbody>
</table>
<section id="ignored-end-of-line">
<span id="string-escape-ignore"></span><h4><span class="section-number">2.5.4.1. </span>Ignored end of line<a class="headerlink" href="#ignored-end-of-line" title="Link to this heading">¶</a></h4>
<p>A backslash can be added at the end of a line to ignore the newline:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'This string will not include </span><span class="se">\</span>
<span class="gp">... </span><span class="s1">backslashes or newline characters.'</span>
<span class="go">'This string will not include backslashes or newline characters.'</span>
</pre></div>
</div>
<p>The same result can be achieved using <a class="reference internal" href="#strings"><span class="std std-ref">triple-quoted strings</span></a>,
or parentheses and <a class="reference internal" href="expressions.html#string-concatenation"><span class="std std-ref">string literal concatenation</span></a>.</p>
</section>
<section id="escaped-characters">
<span id="string-escape-escaped-char"></span><h4><span class="section-number">2.5.4.2. </span>Escaped characters<a class="headerlink" href="#escaped-characters" title="Link to this heading">¶</a></h4>
<p>To include a backslash in a non-<a class="reference internal" href="#raw-strings"><span class="std std-ref">raw</span></a> Python string
literal, it must be doubled. The <code class="docutils literal notranslate"><span class="pre">\\</span></code> escape sequence denotes a single
backslash character:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="s1">'C:</span><span class="se">\\</span><span class="s1">Program Files'</span><span class="p">)</span>
<span class="go">C:\Program Files</span>
</pre></div>
</div>
<p>Similarly, the <code class="docutils literal notranslate"><span class="pre">\'</span></code> and <code class="docutils literal notranslate"><span class="pre">\"</span></code> sequences denote the single and double
quote character, respectively:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="s1">'</span><span class="se">\'</span><span class="s1"> and </span><span class="se">\"</span><span class="s1">'</span><span class="p">)</span>
<span class="go">' and "</span>
</pre></div>
</div>
</section>
<section id="octal-character">
<span id="string-escape-oct"></span><h4><span class="section-number">2.5.4.3. </span>Octal character<a class="headerlink" href="#octal-character" title="Link to this heading">¶</a></h4>
<p>The sequence <code class="samp docutils literal notranslate"><span class="pre">\</span><em><span class="pre">ooo</span></em></code> denotes a <em>character</em> with the octal (base 8)
value <em>ooo</em>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'</span><span class="se">\120</span><span class="s1">'</span>
<span class="go">'P'</span>
</pre></div>
</div>
<p>Up to three octal digits (0 through 7) are accepted.</p>
<p>In a bytes literal, <em>character</em> means a <em>byte</em> with the given value.
In a string literal, it means a Unicode character with the given value.</p>
<div class="versionchanged">
<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> (255) 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>
</div>
<div class="versionchanged">
<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> (255) 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 Python version they will raise 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>
</div>
</section>
<section id="hexadecimal-character">
<span id="string-escape-hex"></span><h4><span class="section-number">2.5.4.4. </span>Hexadecimal character<a class="headerlink" href="#hexadecimal-character" title="Link to this heading">¶</a></h4>
<p>The sequence <code class="samp docutils literal notranslate"><span class="pre">\x</span><em><span class="pre">hh</span></em></code> denotes a <em>character</em> with the hex (base 16)
value <em>hh</em>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'</span><span class="se">\x50</span><span class="s1">'</span>
<span class="go">'P'</span>
</pre></div>
</div>
<p>Unlike in Standard C, exactly two hex digits are required.</p>
<p>In a bytes literal, <em>character</em> means a <em>byte</em> with the given value.
In a string literal, it means a Unicode character with the given value.</p>
</section>
<section id="named-unicode-character">
<span id="string-escape-named"></span><h4><span class="section-number">2.5.4.5. </span>Named Unicode character<a class="headerlink" href="#named-unicode-character" title="Link to this heading">¶</a></h4>
<p>The sequence <code class="samp docutils literal notranslate"><span class="pre">\N{</span><em><span class="pre">name</span></em><span class="pre">}</span></code> denotes a Unicode character
with the given <em>name</em>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'</span><span class="se">\N{LATIN CAPITAL LETTER P}</span><span class="s1">'</span>
<span class="go">'P'</span>
<span class="gp">>>> </span><span class="s1">'</span><span class="se">\N{SNAKE}</span><span class="s1">'</span>
<span class="go">'🐍'</span>
</pre></div>
</div>
<p>This sequence cannot appear in <a class="reference internal" href="#bytes-literal"><span class="std std-ref">bytes literals</span></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.3: </span>Support for <a class="reference external" href="https://www.unicode.org/Public/16.0.0/ucd/NameAliases.txt">name aliases</a>
has been added.</p>
</div>
</section>
<section id="hexadecimal-unicode-characters">
<span id="string-escape-long-hex"></span><h4><span class="section-number">2.5.4.6. </span>Hexadecimal Unicode characters<a class="headerlink" href="#hexadecimal-unicode-characters" title="Link to this heading">¶</a></h4>
<p>These sequences <code class="samp docutils literal notranslate"><span class="pre">\u</span><em><span class="pre">xxxx</span></em></code> and <code class="samp docutils literal notranslate"><span class="pre">\U</span><em><span class="pre">xxxxxxxx</span></em></code> denote the
Unicode character with the given hex (base 16) value.
Exactly four digits are required for <code class="docutils literal notranslate"><span class="pre">\u</span></code>; exactly eight digits are
required for <code class="docutils literal notranslate"><span class="pre">\U</span></code>.
The latter can encode any Unicode character.</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="s1">'</span><span class="se">\u1234</span><span class="s1">'</span>
<span class="go">'ሴ'</span>
<span class="gp">>>> </span><span class="s1">'</span><span class="se">\U0001f40d</span><span class="s1">'</span>
<span class="go">'🐍'</span>
</pre></div>
</div>
<p>These sequences cannot appear in <a class="reference internal" href="#bytes-literal"><span class="std std-ref">bytes literals</span></a>.</p>
</section>
<section id="unrecognized-escape-sequences">
<span id="index-21"></span><h4><span class="section-number">2.5.4.7. </span>Unrecognized escape sequences<a class="headerlink" href="#unrecognized-escape-sequences" title="Link to this heading">¶</a></h4>
<p>Unlike in Standard C, all unrecognized escape sequences are left in the string
unchanged, that is, <em>the backslash is left in the result</em>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="s1">'\q'</span><span class="p">)</span>
<span class="go">\q</span>
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="s1">'\q'</span><span class="p">)</span>
<span class="go">['\\', 'q']</span>
</pre></div>
</div>
<p>Note that for bytes literals, the escape sequences only recognized in string
literals (<code class="docutils literal notranslate"><span class="pre">\N...</span></code>, <code class="docutils literal notranslate"><span class="pre">\u...</span></code>, <code class="docutils literal notranslate"><span class="pre">\U...</span></code>) fall into the category of
unrecognized escapes.</p>
<div class="versionchanged">
<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>
</div>
<div class="versionchanged">
<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 Python version they will raise 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>
</div>
</section>
</section>
<section id="bytes-literals">
<span id="bytes-literal"></span><span id="index-22"></span><h3><span class="section-number">2.5.5. </span>Bytes literals<a class="headerlink" href="#bytes-literals" title="Link to this heading">¶</a></h3>
<p><em class="dfn">Bytes literals</em> 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
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 may only contain ASCII characters; bytes with a numeric value of 128
or greater must be expressed with escape sequences (typically
<a class="reference internal" href="#string-escape-hex"><span class="std std-ref">Hexadecimal character</span></a> or <a class="reference internal" href="#string-escape-oct"><span class="std std-ref">Octal character</span></a>):</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="sa">b</span><span class="s1">'</span><span class="se">\x89</span><span class="s1">PNG</span><span class="se">\r\n\x1a\n</span><span class="s1">'</span>
<span class="go">b'\x89PNG\r\n\x1a\n'</span>
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="sa">b</span><span class="s1">'</span><span class="se">\x89</span><span class="s1">PNG</span><span class="se">\r\n\x1a\n</span><span class="s1">'</span><span class="p">)</span>
<span class="go">[137, 80, 78, 71, 13, 10, 26, 10]</span>
</pre></div>
</div>
<p>Similarly, a zero byte must be expressed using an escape sequence (typically
<code class="docutils literal notranslate"><span class="pre">\0</span></code> or <code class="docutils literal notranslate"><span class="pre">\x00</span></code>).</p>
</section>
<section id="raw-string-literals">
<span id="raw-strings"></span><span id="index-23"></span><h3><span class="section-number">2.5.6. </span>Raw string literals<a class="headerlink" href="#raw-string-literals" title="Link to this heading">¶</a></h3>
<p>Both string and bytes literals may optionally be prefixed with a letter ‘<code class="docutils literal notranslate"><span class="pre">r</span></code>’
or ‘<code class="docutils literal notranslate"><span class="pre">R</span></code>’; such constructs are called <em class="dfn">raw string literals</em>
and <em class="dfn">raw bytes literals</em> respectively and treat backslashes as
literal characters.
As a result, in raw string literals, <a class="reference internal" href="#escape-sequences"><span class="std std-ref">escape sequences</span></a>
are not treated specially:</p>
<div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="sa">r</span><span class="s1">'\d</span><span class="si">{4}</span><span class="s1">-\d</span><span class="si">{2}</span><span class="s1">-\d</span><span class="si">{2}</span><span class="s1">'</span>
<span class="go">'\\d{4}-\\d{2}-\\d{2}'</span>
</pre></div>
</div>
<p>Even in a raw literal, quotes can be escaped with a backslash, but the
backslash remains in the result; for example, <code class="docutils literal notranslate"><span class="pre">r"\""</span></code> is a valid string
literal consisting of two characters: a backslash and a double quote; <code class="docutils literal notranslate"><span class="pre">r"\"</span></code>
is not a valid string literal (even a raw string cannot end in an odd number of
backslashes). Specifically, <em>a raw literal cannot end in a single backslash</em>
(since the backslash would escape the following quote character). Note also
that a single backslash followed by a newline is interpreted as those two
characters as part of the literal, <em>not</em> as a line continuation.</p>
</section>
<section id="formatted-string-literals">
<span id="f-strings"></span><span id="index-24"></span><span id="id13"></span><h3><span class="section-number">2.5.7. </span>f-strings<a class="headerlink" href="#formatted-string-literals" title="Link to this heading">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.6.</span></p>
</div>
<p>A <em class="dfn">formatted string literal</em> or <em class="dfn">f-string</em> is a string literal
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
replacement fields, which are expressions delimited by curly braces <code class="docutils literal notranslate"><span class="pre">{}</span></code>.
While other string literals always have a constant value, formatted strings
are really expressions evaluated at run time.</p>
<p>Escape sequences are decoded like in ordinary string literals (except when
a literal is also marked as a raw string). After decoding, the grammar
for the contents of the string is:</p>
<pre class="highlight">
<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> | <span class="sx">"{{"</span> | <span class="sx">"}}"</span> | <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>)*
<strong id="grammar-token-python-grammar-replacement_field">replacement_field</strong>: <span class="sx">"{"</span> <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> [<span class="sx">"="</span>] [<span class="sx">"!"</span> <a class="reference internal" href="#grammar-token-python-grammar-conversion"><code class="xref docutils literal notranslate"><span class="pre">conversion</span></code></a>] [<span class="sx">":"</span> <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>] <span class="sx">"}"</span>
<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> | <span class="sx">"*"</span> <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>)
(<span class="sx">","</span> <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> | <span class="sx">","</span> <span class="sx">"*"</span> <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>)* [<span class="sx">","</span>]
| <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>
<strong id="grammar-token-python-grammar-conversion">conversion</strong>: <span class="sx">"s"</span> | <span class="sx">"r"</span> | <span class="sx">"a"</span>
<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>)*
<strong id="grammar-token-python-grammar-literal_char">literal_char</strong>: <any code point except <span class="sx">"{"</span>, <span class="sx">"}"</span> or NULL>
</pre>
<p>The parts of the string outside curly braces are treated literally,
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
with the corresponding single curly brace. A single opening curly
bracket <code class="docutils literal notranslate"><span class="pre">'{'</span></code> marks a replacement field, which starts with a
Python expression. To display both the expression text and its value after
evaluation, (useful in debugging), an equal sign <code class="docutils literal notranslate"><span class="pre">'='</span></code> may be added after the
expression. A conversion field, introduced by an exclamation point <code class="docutils literal notranslate"><span class="pre">'!'</span></code> may
follow. A format specifier may also be appended, introduced by a colon <code class="docutils literal notranslate"><span class="pre">':'</span></code>.
A replacement field ends with a closing curly bracket <code class="docutils literal notranslate"><span class="pre">'}'</span></code>.</p>
<p>Expressions in formatted string literals are treated like regular
Python expressions surrounded by parentheses, with a few exceptions.
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
assignment expressions <code class="docutils literal notranslate"><span class="pre">:=</span></code> must be surrounded by explicit parentheses.
Each expression is evaluated in the context where the formatted string literal
appears, in order from left to right. Replacement expressions can contain
newlines in both single-quoted and triple-quoted f-strings and they can contain
comments. Everything that comes after a <code class="docutils literal notranslate"><span class="pre">#</span></code> inside a replacement field
is a comment (even closing braces and quotes). In that case, replacement fields
must be closed in a different line.</p>
<div class="highlight-text notranslate"><div class="highlight"><pre><span></span>>>> f"abc{a # This is a comment }"
... + 3}"
'abc5'
</pre></div>
</div>
<div class="versionchanged">
<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
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
in formatted string literals due to a problem with the implementation.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.12: </span>Prior to Python 3.12, comments were not allowed inside f-string replacement
fields.</p>
</div>
<p>When the equal sign <code class="docutils literal notranslate"><span class="pre">'='</span></code> is provided, the output will have the expression
text, the <code class="docutils literal notranslate"><span class="pre">'='</span></code> and the evaluated value. Spaces after the opening brace
<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
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
provided, unless there is a format specified. When a format is specified it
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
declared.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.8: </span>The equal sign <code class="docutils literal notranslate"><span class="pre">'='</span></code>.</p>
</div>
<p>If a conversion is specified, the result of evaluating the expression
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
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>
<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
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
expression or conversion result. An empty string is passed when the
format specifier is omitted. The formatted result is then included in
the final value of the whole string.</p>
<p>Top-level format specifiers may include nested replacement fields. These nested
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
<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
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>
<p>Formatted string literals may be concatenated, but replacement fields
cannot be split across literals.</p>
<p>Some examples of formatted string literals:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">name</span> <span class="o">=</span> <span class="s2">"Fred"</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"He said his name is </span><span class="si">{</span><span class="n">name</span><span class="si">!r}</span><span class="s2">."</span>
<span class="go">"He said his name is 'Fred'."</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"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">."</span> <span class="c1"># repr() is equivalent to !r</span>
<span class="go">"He said his name is 'Fred'."</span>
<span class="gp">>>> </span><span class="n">width</span> <span class="o">=</span> <span class="mi">10</span>
<span class="gp">>>> </span><span class="n">precision</span> <span class="o">=</span> <span class="mi">4</span>
<span class="gp">>>> </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">"12.34567"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"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">"</span> <span class="c1"># nested fields</span>
<span class="go">'result: 12.35'</span>
<span class="gp">>>> </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>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"</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">"</span> <span class="c1"># using date format specifier</span>
<span class="go">'January 27, 2017'</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"</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">"</span> <span class="c1"># using date format specifier and debugging</span>
<span class="go">'today=January 27, 2017'</span>
<span class="gp">>>> </span><span class="n">number</span> <span class="o">=</span> <span class="mi">1024</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"</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">"</span> <span class="c1"># using integer format specifier</span>
<span class="go">'0x400'</span>
<span class="gp">>>> </span><span class="n">foo</span> <span class="o">=</span> <span class="s2">"bar"</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"</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">"</span> <span class="c1"># preserves whitespace</span>
<span class="go">" foo = 'bar'"</span>
<span class="gp">>>> </span><span class="n">line</span> <span class="o">=</span> <span class="s2">"The mill's closed"</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">line</span><span class="w"> </span><span class="si">= }</span><span class="s2">"</span>
<span class="go">'line = "The mill\'s closed"'</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"</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">"</span>
<span class="go">"line = The mill's closed "</span>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"</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">"</span>
<span class="go">'line = "The mill\'s closed" '</span>
</pre></div>
</div>
<p>Reusing the outer f-string quoting type inside a replacement field is
permitted:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>
<span class="gp">>>> </span><span class="sa">f</span><span class="s2">"abc </span><span class="si">{</span><span class="n">a</span><span class="p">[</span><span class="s2">"x"</span><span class="p">]</span><span class="si">}</span><span class="s2"> def"</span>
<span class="go">'abc 2 def'</span>
</pre></div>
</div>
<div class="versionchanged">
<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
inside a replacement field was not possible.</p>
</div>
<p>Backslashes are also allowed in replacement fields and are evaluated the same
way as in any other context:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">]</span>
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"List a contains:</span><span class="se">\n</span><span class="si">{</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</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">"</span><span class="p">)</span>
<span class="go">List a contains:</span>
<span class="go">a</span>
<span class="go">b</span>
<span class="go">c</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.12: </span>Prior to Python 3.12, backslashes were not permitted inside an f-string
replacement field.</p>
</div>
<p>Formatted string literals cannot be used as docstrings, even if they do not
include expressions.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span><span class="w"> </span><span class="nf">foo</span><span class="p">():</span>
<span class="gp">... </span> <span class="sa">f</span><span class="s2">"Not a docstring"</span>
<span class="gp">...</span>
<span class="gp">>>> </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>
<span class="go">True</span>
</pre></div>
</div>
<p>See also <span class="target" id="index-25"></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,
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>
</section>
<section id="template-string-literals">
<span id="t-strings"></span><span id="id14"></span><h3><span class="section-number">2.5.8. </span>t-strings<a class="headerlink" href="#template-string-literals" title="Link to this heading">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.14.</span></p>
</div>
<p>A <em class="dfn">template string literal</em> or <em class="dfn">t-string</em> is a string literal
that is prefixed with ‘<code class="docutils literal notranslate"><span class="pre">t</span></code>’ or ‘<code class="docutils literal notranslate"><span class="pre">T</span></code>’.
These strings follow the same syntax and evaluation rules as
<a class="reference internal" href="#f-strings"><span class="std std-ref">formatted string literals</span></a>, with the following differences:</p>
<ul class="simple">
<li><p>Rather than evaluating to a <code class="docutils literal notranslate"><span class="pre">str</span></code> object, template string literals evaluate
to a <a class="reference internal" href="../library/string.templatelib.html#string.templatelib.Template" title="string.templatelib.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">string.templatelib.Template</span></code></a> object.</p></li>
<li><p>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 is not used.
Instead, the format specifier and conversions (if any) are passed to
a new <a class="reference internal" href="../library/string.templatelib.html#string.templatelib.Interpolation" title="string.templatelib.Interpolation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Interpolation</span></code></a> object that is created
for each evaluated expression.
It is up to code that processes the resulting <a class="reference internal" href="../library/string.templatelib.html#string.templatelib.Template" title="string.templatelib.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a>
object to decide how to handle format specifiers and conversions.</p></li>
<li><p>Format specifiers containing nested replacement fields are evaluated eagerly,
prior to being passed to the <a class="reference internal" href="../library/string.templatelib.html#string.templatelib.Interpolation" title="string.templatelib.Interpolation"><code class="xref py py-class docutils literal notranslate"><span class="pre">Interpolation</span></code></a> object.
For instance, an interpolation of the form <code class="docutils literal notranslate"><span class="pre">{amount:.{precision}f}</span></code> will
evaluate the inner expression <code class="docutils literal notranslate"><span class="pre">{precision}</span></code> to determine the value of the
<code class="docutils literal notranslate"><span class="pre">format_spec</span></code> attribute.
If <code class="docutils literal notranslate"><span class="pre">precision</span></code> were to be <code class="docutils literal notranslate"><span class="pre">2</span></code>, the resulting format specifier
would be <code class="docutils literal notranslate"><span class="pre">'.2f'</span></code>.</p></li>
<li><p>When the equals sign <code class="docutils literal notranslate"><span class="pre">'='</span></code> is provided in an interpolation expression,
the text of the expression is appended to the literal string that precedes
the relevant interpolation.
This includes the equals sign and any surrounding whitespace.
The <code class="xref py py-class docutils literal notranslate"><span class="pre">Interpolation</span></code> instance for the expression will be created as
normal, except that <a class="reference internal" href="../library/string.templatelib.html#string.templatelib.Interpolation.conversion" title="string.templatelib.Interpolation.conversion"><code class="xref py py-attr docutils literal notranslate"><span class="pre">conversion</span></code></a> will
be set to ‘<code class="docutils literal notranslate"><span class="pre">r</span></code>’ (<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>) by default.
If an explicit conversion or format specifier are provided,
this will override the default behaviour.</p></li>
</ul>
</section>
</section>
<section id="numeric-literals">
<span id="numbers"></span><h2><span class="section-number">2.6. </span>Numeric literals<a class="headerlink" href="#numeric-literals" title="Link to this heading">¶</a></h2>
<p id="index-26"><a class="reference internal" href="../library/token.html#token.NUMBER" title="token.NUMBER"><code class="xref py py-data docutils literal notranslate"><span class="pre">NUMBER</span></code></a> tokens represent numeric literals, of which there are
three types: integers, floating-point numbers, and imaginary numbers.</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-NUMBER">NUMBER</strong>: <a class="reference internal" href="#grammar-token-python-grammar-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> | <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-imagnumber"><code class="xref docutils literal notranslate"><span class="pre">imagnumber</span></code></a>
</pre>
<p>The numeric value of a numeric literal is the same as if it were passed as a
string to the <a class="reference internal" href="../library/functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>, <a class="reference internal" href="../library/functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> or <a class="reference internal" href="../library/functions.html#complex" title="complex"><code class="xref py py-class docutils literal notranslate"><span class="pre">complex</span></code></a> class
constructor, respectively.
Note that not all valid inputs for those constructors are also valid literals.</p>
<p>Numeric literals do not include a sign; a phrase like <code class="docutils literal notranslate"><span class="pre">-1</span></code> is
actually an expression composed of the unary operator ‘<code class="docutils literal notranslate"><span class="pre">-</span></code>’ and the literal
<code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
<section id="integer-literals">
<span id="integers"></span><span id="index-27"></span><h3><span class="section-number">2.6.1. </span>Integer literals<a class="headerlink" href="#integer-literals" title="Link to this heading">¶</a></h3>
<p>Integer literals denote whole numbers. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">7</span>
<span class="mi">3</span>
<span class="mi">2147483647</span>
</pre></div>
</div>
<p>There is no limit for the length of integer literals apart from what can be
stored in available memory:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">7922816251426433759354395033679228162514264337593543950336</span>
</pre></div>
</div>
<p>Underscores can be used to group digits for enhanced readability,
and are ignored for determining the numeric value of the literal.
For example, the following literals are equivalent:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">100_000_000_000</span>
<span class="mi">100000000000</span>
<span class="mi">1_00_00_00_00_000</span>
</pre></div>
</div>
<p>Underscores can only occur between digits.
For example, <code class="docutils literal notranslate"><span class="pre">_123</span></code>, <code class="docutils literal notranslate"><span class="pre">321_</span></code>, and <code class="docutils literal notranslate"><span class="pre">123__321</span></code> are <em>not</em> valid literals.</p>
<p>Integers can be specified in binary (base 2), octal (base 8), or hexadecimal
(base 16) using the prefixes <code class="docutils literal notranslate"><span class="pre">0b</span></code>, <code class="docutils literal notranslate"><span class="pre">0o</span></code> and <code class="docutils literal notranslate"><span class="pre">0x</span></code>, respectively.
Hexadecimal digits 10 through 15 are represented by letters <code class="docutils literal notranslate"><span class="pre">A</span></code>-<code class="docutils literal notranslate"><span class="pre">F</span></code>,
case-insensitive. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mb">0b100110111</span>
<span class="mb">0b_1110_0101</span>
<span class="mo">0o177</span>
<span class="mo">0o377</span>
<span class="mh">0xdeadbeef</span>
<span class="mh">0xDead_Beef</span>
</pre></div>
</div>
<p>An underscore can follow the base specifier.
For example, <code class="docutils literal notranslate"><span class="pre">0x_1f</span></code> is a valid literal, but <code class="docutils literal notranslate"><span class="pre">0_x1f</span></code> and <code class="docutils literal notranslate"><span class="pre">0x__1f</span></code> are
not.</p>
<p>Leading zeros in a non-zero decimal number are not allowed.
For example, <code class="docutils literal notranslate"><span class="pre">0123</span></code> is not a valid literal.
This is for disambiguation with C-style octal literals, which Python used
before version 3.0.</p>
<p>Formally, integer literals are described by the following lexical definitions:</p>
<pre class="highlight">
<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> | <a class="reference internal" href="#grammar-token-python-grammar-zerointeger"><code class="xref docutils literal notranslate"><span class="pre">zerointeger</span></code></a>
<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> ([<span class="sx">"_"</span>] <a class="reference internal" href="#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>)*
<strong id="grammar-token-python-grammar-bininteger">bininteger</strong>: <span class="sx">"0"</span> (<span class="sx">"b"</span> | <span class="sx">"B"</span>) ([<span class="sx">"_"</span>] <a class="reference internal" href="#grammar-token-python-grammar-bindigit"><code class="xref docutils literal notranslate"><span class="pre">bindigit</span></code></a>)+
<strong id="grammar-token-python-grammar-octinteger">octinteger</strong>: <span class="sx">"0"</span> (<span class="sx">"o"</span> | <span class="sx">"O"</span>) ([<span class="sx">"_"</span>] <a class="reference internal" href="#grammar-token-python-grammar-octdigit"><code class="xref docutils literal notranslate"><span class="pre">octdigit</span></code></a>)+
<strong id="grammar-token-python-grammar-hexinteger">hexinteger</strong>: <span class="sx">"0"</span> (<span class="sx">"x"</span> | <span class="sx">"X"</span>) ([<span class="sx">"_"</span>] <a class="reference internal" href="#grammar-token-python-grammar-hexdigit"><code class="xref docutils literal notranslate"><span class="pre">hexdigit</span></code></a>)+
<strong id="grammar-token-python-grammar-zerointeger">zerointeger</strong>: <span class="sx">"0"</span>+ ([<span class="sx">"_"</span>] <span class="sx">"0"</span>)*
<strong id="grammar-token-python-grammar-nonzerodigit">nonzerodigit</strong>: <span class="sx">"1"</span>...<span class="sx">"9"</span>
<strong id="grammar-token-python-grammar-digit">digit</strong>: <span class="sx">"0"</span>...<span class="sx">"9"</span>
<strong id="grammar-token-python-grammar-bindigit">bindigit</strong>: <span class="sx">"0"</span> | <span class="sx">"1"</span>
<strong id="grammar-token-python-grammar-octdigit">octdigit</strong>: <span class="sx">"0"</span>...<span class="sx">"7"</span>
<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> | <span class="sx">"a"</span>...<span class="sx">"f"</span> | <span class="sx">"A"</span>...<span class="sx">"F"</span>
</pre>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.6: </span>Underscores are now allowed for grouping purposes in literals.</p>
</div>
</section>
<section id="floating-point-literals">
<span id="floating"></span><span id="index-28"></span><h3><span class="section-number">2.6.2. </span>Floating-point literals<a class="headerlink" href="#floating-point-literals" title="Link to this heading">¶</a></h3>
<p>Floating-point (float) literals, such as <code class="docutils literal notranslate"><span class="pre">3.14</span></code> or <code class="docutils literal notranslate"><span class="pre">1.5</span></code>, denote
<a class="reference internal" href="datamodel.html#datamodel-float"><span class="std std-ref">approximations of real numbers</span></a>.</p>
<p>They consist of <em>integer</em> and <em>fraction</em> parts, each composed of decimal digits.
The parts are separated by a decimal point, <code class="docutils literal notranslate"><span class="pre">.</span></code>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">2.71828</span>
<span class="mf">4.0</span>
</pre></div>
</div>
<p>Unlike in integer literals, leading zeros are allowed in the numeric parts.
For example, <code class="docutils literal notranslate"><span class="pre">077.010</span></code> is legal, and denotes the same number as <code class="docutils literal notranslate"><span class="pre">77.10</span></code>.</p>
<p>As in integer literals, single underscores may occur between digits to help
readability:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">96_485.332_123</span>
<span class="mf">3.14_15_93</span>
</pre></div>
</div>
<p>Either of these parts, but not both, can be empty. For example:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">10.</span> <span class="c1"># (equivalent to 10.0)</span>
<span class="mf">.001</span> <span class="c1"># (equivalent to 0.001)</span>
</pre></div>
</div>
<p>Optionally, the integer and fraction may be followed by an <em>exponent</em>:
the letter <code class="docutils literal notranslate"><span class="pre">e</span></code> or <code class="docutils literal notranslate"><span class="pre">E</span></code>, followed by an optional sign, <code class="docutils literal notranslate"><span class="pre">+</span></code> or <code class="docutils literal notranslate"><span class="pre">-</span></code>,
and a number in the same format as the integer and fraction parts.
The <code class="docutils literal notranslate"><span class="pre">e</span></code> or <code class="docutils literal notranslate"><span class="pre">E</span></code> represents “times ten raised to the power of”:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">1.0e3</span> <span class="c1"># (represents 1.0×10³, or 1000.0)</span>
<span class="mf">1.166e-5</span> <span class="c1"># (represents 1.166×10⁻⁵, or 0.00001166)</span>
<span class="mf">6.02214076e+23</span> <span class="c1"># (represents 6.02214076×10²³, or 602214076000000000000000.)</span>
</pre></div>
</div>
<p>In floats with only integer and exponent parts, the decimal point may be
omitted:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">1e3</span> <span class="c1"># (equivalent to 1.e3 and 1.0e3)</span>
<span class="mf">0e0</span> <span class="c1"># (equivalent to 0.)</span>
</pre></div>
</div>
<p>Formally, floating-point literals are described by the following
lexical definitions:</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-floatnumber">floatnumber</strong>:
| <a class="reference internal" href="#grammar-token-python-grammar-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a> <span class="sx">"."</span> [<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-exponent"><code class="xref docutils literal notranslate"><span class="pre">exponent</span></code></a>]
| <span class="sx">"."</span> <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-exponent"><code class="xref docutils literal notranslate"><span class="pre">exponent</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> <a class="reference internal" href="#grammar-token-python-grammar-exponent"><code class="xref docutils literal notranslate"><span class="pre">exponent</span></code></a>
<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> ([<span class="sx">"_"</span>] <a class="reference internal" href="#grammar-token-python-grammar-digit"><code class="xref docutils literal notranslate"><span class="pre">digit</span></code></a>)*
<strong id="grammar-token-python-grammar-exponent">exponent</strong>: (<span class="sx">"e"</span> | <span class="sx">"E"</span>) [<span class="sx">"+"</span> | <span class="sx">"-"</span>] <a class="reference internal" href="#grammar-token-python-grammar-digitpart"><code class="xref docutils literal notranslate"><span class="pre">digitpart</span></code></a>
</pre>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.6: </span>Underscores are now allowed for grouping purposes in literals.</p>
</div>
</section>
<section id="imaginary-literals">
<span id="imaginary"></span><span id="index-29"></span><h3><span class="section-number">2.6.3. </span>Imaginary literals<a class="headerlink" href="#imaginary-literals" title="Link to this heading">¶</a></h3>
<p>Python has <a class="reference internal" href="../library/stdtypes.html#typesnumeric"><span class="std std-ref">complex number</span></a> objects, but no complex
literals.
Instead, <em>imaginary literals</em> denote complex numbers with a zero
real part.</p>
<p>For example, in math, the complex number 3+4.2<em>i</em> is written
as the real number 3 added to the imaginary number 4.2<em>i</em>.
Python uses a similar syntax, except the imaginary unit is written as <code class="docutils literal notranslate"><span class="pre">j</span></code>
rather than <em>i</em>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">3</span><span class="o">+</span><span class="mf">4.2</span><span class="n">j</span>
</pre></div>
</div>
<p>This is an expression composed
of the <a class="reference internal" href="#integers"><span class="std std-ref">integer literal</span></a> <code class="docutils literal notranslate"><span class="pre">3</span></code>,
the <a class="reference internal" href="#operators"><span class="std std-ref">operator</span></a> ‘<code class="docutils literal notranslate"><span class="pre">+</span></code>’,
and the <a class="reference internal" href="#imaginary"><span class="std std-ref">imaginary literal</span></a> <code class="docutils literal notranslate"><span class="pre">4.2j</span></code>.
Since these are three separate tokens, whitespace is allowed between them:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">3</span> <span class="o">+</span> <span class="mf">4.2</span><span class="n">j</span>
</pre></div>
</div>
<p>No whitespace is allowed <em>within</em> each token.
In particular, the <code class="docutils literal notranslate"><span class="pre">j</span></code> suffix, may not be separated from the number
before it.</p>
<p>The number before the <code class="docutils literal notranslate"><span class="pre">j</span></code> has the same syntax as a floating-point literal.
Thus, the following are valid imaginary literals:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">4.2</span><span class="n">j</span>
<span class="mf">3.14</span><span class="n">j</span>
<span class="mf">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>
</pre></div>
</div>
<p>Unlike in a floating-point literal the decimal point can be omitted if the
imaginary number only has an integer part.
The number is still evaluated as a floating-point number, not an integer:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mi">10</span><span class="n">j</span>
<span class="mi">0</span><span class="n">j</span>
<span class="mi">1000000000000000000000000</span><span class="n">j</span> <span class="c1"># equivalent to 1e+24j</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">j</span></code> suffix is case-insensitive.
That means you can use <code class="docutils literal notranslate"><span class="pre">J</span></code> instead:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="mf">3.14</span><span class="n">J</span> <span class="c1"># equivalent to 3.14j</span>
</pre></div>
</div>
<p>Formally, imaginary literals are described by the following lexical definition:</p>
<pre class="highlight">
<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>) (<span class="sx">"j"</span> | <span class="sx">"J"</span>)
</pre>
</section>
</section>
<section id="operators-and-delimiters">
<span id="lexical-ellipsis"></span><span id="operators"></span><span id="delimiters"></span><h2><span class="section-number">2.7. </span>Operators and delimiters<a class="headerlink" href="#operators-and-delimiters" title="Link to this heading">¶</a></h2>
<p id="index-30">The following grammar defines <em class="dfn">operator</em> and <em class="dfn">delimiter</em> tokens,
that is, the generic <a class="reference internal" href="../library/token.html#token.OP" title="token.OP"><code class="xref py py-data docutils literal notranslate"><span class="pre">OP</span></code></a> token type.
A <a class="reference internal" href="../library/token.html#token-operators-delimiters"><span class="std std-ref">list of these tokens and their names</span></a>
is also available in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">token</span></code> module documentation.</p>
<pre class="highlight">
<strong id="grammar-token-python-grammar-OP">OP</strong>:
| assignment_operator
| bitwise_operator
| comparison_operator
| enclosing_delimiter
| other_delimiter
| arithmetic_operator
| <span class="sx">"..."</span>
| other_op
<strong id="grammar-token-python-grammar-assignment_operator">assignment_operator</strong>: <span class="sx">"+="</span> | <span class="sx">"-="</span> | <span class="sx">"*="</span> | <span class="sx">"**="</span> | <span class="sx">"/="</span> | <span class="sx">"//="</span> | <span class="sx">"%="</span> |
<span class="sx">"&="</span> | <span class="sx">"|="</span> | <span class="sx">"^="</span> | <span class="sx">"<<="</span> | <span class="sx">">>="</span> | <span class="sx">"@="</span> | <span class="sx">":="</span>
<strong id="grammar-token-python-grammar-bitwise_operator">bitwise_operator</strong>: <span class="sx">"&"</span> | <span class="sx">"|"</span> | <span class="sx">"^"</span> | <span class="sx">"~"</span> | <span class="sx">"<<"</span> | <span class="sx">">>"</span>
<strong id="grammar-token-python-grammar-comparison_operator">comparison_operator</strong>: <span class="sx">"<="</span> | <span class="sx">">="</span> | <span class="sx">"<"</span> | <span class="sx">">"</span> | <span class="sx">"=="</span> | <span class="sx">"!="</span>
<strong id="grammar-token-python-grammar-enclosing_delimiter">enclosing_delimiter</strong>: <span class="sx">"("</span> | <span class="sx">")"</span> | <span class="sx">"["</span> | <span class="sx">"]"</span> | <span class="sx">"{"</span> | <span class="sx">"}"</span>
<strong id="grammar-token-python-grammar-other_delimiter">other_delimiter</strong>: <span class="sx">","</span> | <span class="sx">":"</span> | <span class="sx">"!"</span> | <span class="sx">";"</span> | <span class="sx">"="</span> | <span class="sx">"->"</span>
<strong id="grammar-token-python-grammar-arithmetic_operator">arithmetic_operator</strong>: <span class="sx">"+"</span> | <span class="sx">"-"</span> | <span class="sx">"**"</span> | <span class="sx">"*"</span> | <span class="sx">"//"</span> | <span class="sx">"/"</span> | <span class="sx">"%"</span>
<strong id="grammar-token-python-grammar-other_op">other_op</strong>: <span class="sx">"."</span> | <span class="sx">"@"</span>
</pre>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Generally, <em>operators</em> are used to combine <a class="reference internal" href="expressions.html#expressions"><span class="std std-ref">expressions</span></a>,
while <em>delimiters</em> serve other purposes.
However, there is no clear, formal distinction between the two categories.</p>
<p>Some tokens can serve as either operators or delimiters, depending on usage.
For example, <code class="docutils literal notranslate"><span class="pre">*</span></code> is both the multiplication operator and a delimiter used
for sequence unpacking, and <code class="docutils literal notranslate"><span class="pre">@</span></code> is both the matrix multiplication and
a delimiter that introduces decorators.</p>
<p>For some tokens, the distinction is unclear.
For example, some people consider <code class="docutils literal notranslate"><span class="pre">.</span></code>, <code class="docutils literal notranslate"><span class="pre">(</span></code>, and <code class="docutils literal notranslate"><span class="pre">)</span></code> to be delimiters, while others
see the <a class="reference internal" href="../library/functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> operator and the function call operator(s).</p>
<p>Some of Python’s operators, like <code class="docutils literal notranslate"><span class="pre">and</span></code>, <code class="docutils literal notranslate"><span class="pre">or</span></code>, and <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code>, use
<a class="reference internal" href="#keywords"><span class="std std-ref">keyword</span></a> tokens rather than “symbols” (operator tokens).</p>
</div>
<p>A sequence of three consecutive periods (<code class="docutils literal notranslate"><span class="pre">...</span></code>) has a special
meaning as an <a class="reference internal" href="../library/constants.html#Ellipsis" title="Ellipsis"><code class="xref py py-data docutils literal notranslate"><span class="pre">Ellipsis</span></code></a> literal.</p>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<div>
<h3><a href="../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">2. Lexical analysis</a><ul>
<li><a class="reference internal" href="#line-structure">2.1. Line structure</a><ul>
<li><a class="reference internal" href="#logical-lines">2.1.1. Logical lines</a></li>
<li><a class="reference internal" href="#physical-lines">2.1.2. Physical lines</a></li>
<li><a class="reference internal" href="#comments">2.1.3. Comments</a></li>
<li><a class="reference internal" href="#encoding-declarations">2.1.4. Encoding declarations</a></li>
<li><a class="reference internal" href="#explicit-line-joining">2.1.5. Explicit line joining</a></li>
<li><a class="reference internal" href="#implicit-line-joining">2.1.6. Implicit line joining</a></li>
<li><a class="reference internal" href="#blank-lines">2.1.7. Blank lines</a></li>
<li><a class="reference internal" href="#indentation">2.1.8. Indentation</a></li>
<li><a class="reference internal" href="#whitespace-between-tokens">2.1.9. Whitespace between tokens</a></li>
<li><a class="reference internal" href="#end-marker">2.1.10. End marker</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-tokens">2.2. Other tokens</a></li>
<li><a class="reference internal" href="#names-identifiers-and-keywords">2.3. Names (identifiers and keywords)</a><ul>
<li><a class="reference internal" href="#keywords">2.3.1. Keywords</a></li>
<li><a class="reference internal" href="#soft-keywords">2.3.2. Soft Keywords</a></li>
<li><a class="reference internal" href="#reserved-classes-of-identifiers">2.3.3. Reserved classes of identifiers</a></li>
</ul>
</li>
<li><a class="reference internal" href="#literals">2.4. Literals</a></li>
<li><a class="reference internal" href="#string-and-bytes-literals">2.5. String and Bytes literals</a><ul>
<li><a class="reference internal" href="#triple-quoted-strings">2.5.1. Triple-quoted strings</a></li>
<li><a class="reference internal" href="#string-prefixes">2.5.2. String prefixes</a></li>
<li><a class="reference internal" href="#formal-grammar">2.5.3. Formal grammar</a></li>
<li><a class="reference internal" href="#escape-sequences">2.5.4. Escape sequences</a><ul>
<li><a class="reference internal" href="#ignored-end-of-line">2.5.4.1. Ignored end of line</a></li>
<li><a class="reference internal" href="#escaped-characters">2.5.4.2. Escaped characters</a></li>
<li><a class="reference internal" href="#octal-character">2.5.4.3. Octal character</a></li>
<li><a class="reference internal" href="#hexadecimal-character">2.5.4.4. Hexadecimal character</a></li>
<li><a class="reference internal" href="#named-unicode-character">2.5.4.5. Named Unicode character</a></li>
<li><a class="reference internal" href="#hexadecimal-unicode-characters">2.5.4.6. Hexadecimal Unicode characters</a></li>
<li><a class="reference internal" href="#unrecognized-escape-sequences">2.5.4.7. Unrecognized escape sequences</a></li>
</ul>
</li>
<li><a class="reference internal" href="#bytes-literals">2.5.5. Bytes literals</a></li>
<li><a class="reference internal" href="#raw-string-literals">2.5.6. Raw string literals</a></li>
<li><a class="reference internal" href="#formatted-string-literals">2.5.7. f-strings</a></li>
<li><a class="reference internal" href="#template-string-literals">2.5.8. t-strings</a></li>
</ul>
</li>
<li><a class="reference internal" href="#numeric-literals">2.6. Numeric literals</a><ul>
<li><a class="reference internal" href="#integer-literals">2.6.1. Integer literals</a></li>
<li><a class="reference internal" href="#floating-point-literals">2.6.2. Floating-point literals</a></li>
<li><a class="reference internal" href="#imaginary-literals">2.6.3. Imaginary literals</a></li>
</ul>
</li>
<li><a class="reference internal" href="#operators-and-delimiters">2.7. Operators and delimiters</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="introduction.html"
title="previous chapter"><span class="section-number">1. </span>Introduction</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="datamodel.html"
title="next chapter"><span class="section-number">3. </span>Data model</a></p>
</div>
<div role="note" aria-label="source link">
<h3>This page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/main/Doc/reference/lexical_analysis.rst?plain=1"
rel="nofollow">Show source
</a>
</li>
</ul>
</div>
</div>
<div id="sidebarbutton" title="Collapse sidebar">
<span>«</span>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="datamodel.html" title="3. Data model"
>next</a> |</li>
<li class="right" >
<a href="introduction.html" title="1. Introduction"
>previous</a> |</li>
<li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.14.0 Documentation</a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Language Reference</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="section-number">2. </span>Lexical analysis</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">
<input type="submit" value="Go">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.
<br>
This page is licensed under the Python Software Foundation License Version 2.
<br>
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br>
See <a href="/license.html">History and License</a> for more information.<br>
<br>
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br>
<br>
Last updated on Oct 07, 2025 (10:02 UTC).
<a href="/bugs.html">Found a bug</a>?
<br>
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
</div>
</body>
</html>