summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/reference%2Flexical_analysis.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/reference%2Flexical_analysis.html
new repository
Diffstat (limited to 'devdocs/python~3.12/reference%2Flexical_analysis.html')
-rw-r--r--devdocs/python~3.12/reference%2Flexical_analysis.html235
1 files changed, 235 insertions, 0 deletions
diff --git a/devdocs/python~3.12/reference%2Flexical_analysis.html b/devdocs/python~3.12/reference%2Flexical_analysis.html
new file mode 100644
index 00000000..c9356e34
--- /dev/null
+++ b/devdocs/python~3.12/reference%2Flexical_analysis.html
@@ -0,0 +1,235 @@
+ <span id="lexical"></span><h1> Lexical analysis</h1> <p id="index-0">A Python program is read by a <em>parser</em>. Input to the parser is a stream of <em>tokens</em>, generated by the <em>lexical analyzer</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#SyntaxError" title="SyntaxError"><code>SyntaxError</code></a> is raised.</p> <section id="line-structure"> <span id="id1"></span><h2>
+<span class="section-number">2.1. </span>Line structure</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</h3> <p id="index-3">The end of a logical line is represented by the token NEWLINE. Statements cannot cross logical line boundaries except where NEWLINE 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 explicit or implicit <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</h3> <p>A physical line is a sequence of characters terminated by an end-of-line sequence. In source files and strings, any of the standard platform line termination sequences can be used - the Unix form using ASCII LF (linefeed), the Windows form using the ASCII sequence CR LF (return followed by linefeed), or the old Macintosh form using the ASCII CR (return) character. All of these forms can be used equally, regardless of platform. The end of input also serves as an implicit terminator for the final physical line.</p> <p>When embedding Python, source code strings should be passed to Python APIs using the standard C conventions for newline characters (the <code>\n</code> character, representing ASCII LF, is the line terminator).</p> </section> <section id="comments"> <span id="id4"></span><h3>
+<span class="section-number">2.1.3. </span>Comments</h3> <p id="index-4">A comment starts with a hash character (<code>#</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</h3> <p id="index-5">If a comment in the first or second line of the Python script matches the regular expression <code>coding[=:]\s*([-\w.]+)</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> <pre data-language="python"># -*- coding: &lt;encoding-name&gt; -*-
+</pre> <p>which is recognized also by GNU Emacs, and</p> <pre data-language="python"># vim:fileencoding=&lt;encoding-name&gt;
+</pre> <p>which is recognized by Bram Moolenaar’s VIM.</p> <p>If no encoding declaration is found, the default encoding is UTF-8. In addition, if the first bytes of the file are the UTF-8 byte-order mark (<code>b'\xef\xbb\xbf'</code>), the declared file encoding is UTF-8 (this is supported, among others, by Microsoft’s <strong class="program">notepad</strong>).</p> <p>If an encoding is declared, the encoding name must be recognized by Python (see <a class="reference internal" href="../library/codecs#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> </section> <section id="explicit-line-joining"> <span id="explicit-joining"></span><h3>
+<span class="section-number">2.1.5. </span>Explicit line joining</h3> <p id="index-6">Two or more physical lines may be joined into logical lines using backslash characters (<code>\</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> <pre data-language="python">if 1900 &lt; year &lt; 2100 and 1 &lt;= month &lt;= 12 \
+ and 1 &lt;= day &lt;= 31 and 0 &lt;= hour &lt; 24 \
+ and 0 &lt;= minute &lt; 60 and 0 &lt;= second &lt; 60: # Looks like a valid date
+ return 1
+</pre> <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</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> <pre data-language="python">month_names = ['Januari', 'Februari', 'Maart', # These are the
+ 'April', 'Mei', 'Juni', # Dutch names
+ 'Juli', 'Augustus', 'September', # for the months
+ 'Oktober', 'November', 'December'] # of the year
+</pre> <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="id5"></span><h3>
+<span class="section-number">2.1.7. </span>Blank lines</h3> <p id="index-7">A logical line that contains only spaces, tabs, formfeeds and possibly a comment, is ignored (i.e., no NEWLINE 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 (i.e. one containing not even whitespace or a comment) terminates a multi-line statement.</p> </section> <section id="indentation"> <span id="id6"></span><h3>
+<span class="section-number">2.1.8. </span>Indentation</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#TabError" title="TabError"><code>TabError</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 INDENT and DEDENT 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 INDENT 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 DEDENT token is generated. At the end of the file, a DEDENT 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> <pre data-language="python">def perm(l):
+ # Compute the list of all permutations of l
+ if len(l) &lt;= 1:
+ return [l]
+ r = []
+ for i in range(len(l)):
+ s = l[:i] + l[i+1:]
+ p = perm(s)
+ for x in p:
+ r.append(l[i:i+1] + x)
+ return r
+</pre> <p>The following example shows various indentation errors:</p> <pre data-language="python"> def perm(l): # error: first line indented
+for i in range(len(l)): # error: not indented
+ s = l[:i] + l[i+1:]
+ p = perm(l[:i] + l[i+1:]) # error: unexpected indent
+ for x in p:
+ r.append(l[i:i+1] + x)
+ return r # error: inconsistent dedent
+</pre> <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>return r</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</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 (e.g., ab is one token, but a b is two tokens).</p> </section> </section> <section id="other-tokens"> <span id="id7"></span><h2>
+<span class="section-number">2.2. </span>Other tokens</h2> <p>Besides NEWLINE, INDENT and DEDENT, the following categories of tokens exist: <em>identifiers</em>, <em>keywords</em>, <em>literals</em>, <em>operators</em>, and <em>delimiters</em>. Whitespace characters (other than 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="identifiers"> <span id="identifiers-and-keywords"></span><h2>
+<span class="section-number">2.3. </span>Identifiers and keywords</h2> <p id="index-10">Identifiers (also referred to as <em>names</em>) are described by the following lexical definitions.</p> <p>The syntax of identifiers in Python is based on the Unicode standard annex UAX-31, with elaboration and changes as defined below; see also <span class="target" id="index-11"></span><a class="pep reference external" href="https://peps.python.org/pep-3131/"><strong>PEP 3131</strong></a> for further details.</p> <p>Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters <code>A</code> through <code>Z</code>, the underscore <code>_</code> and, except for the first character, the digits <code>0</code> through <code>9</code>.</p> <p>Python 3.0 introduces additional characters from outside the ASCII range (see <span class="target" id="index-12"></span><a class="pep reference external" href="https://peps.python.org/pep-3131/"><strong>PEP 3131</strong></a>). For these characters, the classification uses the version of the Unicode Character Database as included in the <a class="reference internal" href="../library/unicodedata#module-unicodedata" title="unicodedata: Access the Unicode Database."><code>unicodedata</code></a> module.</p> <p>Identifiers are unlimited in length. Case is significant.</p> <pre>
+<strong id="grammar-token-python-grammar-identifier"><span id="grammar-token-identifier"></span>identifier </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-xid_start">xid_start</a> <a class="reference internal" href="#grammar-token-python-grammar-xid_continue">xid_continue</a>*
+<strong id="grammar-token-python-grammar-id_start"><span id="grammar-token-id-start"></span>id_start </strong> ::= &lt;all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property&gt;
+<strong id="grammar-token-python-grammar-id_continue"><span id="grammar-token-id-continue"></span>id_continue </strong> ::= &lt;all characters in <a class="reference internal" href="#grammar-token-python-grammar-id_start">id_start</a>, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property&gt;
+<strong id="grammar-token-python-grammar-xid_start"><span id="grammar-token-xid-start"></span>xid_start </strong> ::= &lt;all characters in <a class="reference internal" href="#grammar-token-python-grammar-id_start">id_start</a> whose NFKC normalization is in "id_start xid_continue*"&gt;
+<strong id="grammar-token-python-grammar-xid_continue"><span id="grammar-token-xid-continue"></span>xid_continue</strong> ::= &lt;all characters in <a class="reference internal" href="#grammar-token-python-grammar-id_continue">id_continue</a> whose NFKC normalization is in "id_continue*"&gt;
+</pre> <p>The Unicode category codes mentioned above stand for:</p> <ul class="simple"> <li>
+<em>Lu</em> - uppercase letters</li> <li>
+<em>Ll</em> - lowercase letters</li> <li>
+<em>Lt</em> - titlecase letters</li> <li>
+<em>Lm</em> - modifier letters</li> <li>
+<em>Lo</em> - other letters</li> <li>
+<em>Nl</em> - letter numbers</li> <li>
+<em>Mn</em> - nonspacing marks</li> <li>
+<em>Mc</em> - spacing combining marks</li> <li>
+<em>Nd</em> - decimal numbers</li> <li>
+<em>Pc</em> - connector punctuations</li> <li>
+<em>Other_ID_Start</em> - explicit list of characters in <a class="reference external" href="https://www.unicode.org/Public/15.0.0/ucd/PropList.txt">PropList.txt</a> to support backwards compatibility</li> <li>
+<em>Other_ID_Continue</em> - likewise</li> </ul> <p>All identifiers are converted into the normal form NFKC while parsing; comparison of identifiers is based on NFKC.</p> <p>A non-normative HTML file listing all valid identifier characters for Unicode 15.0.0 can be found at <a class="reference external" href="https://www.unicode.org/Public/15.0.0/ucd/DerivedCoreProperties.txt">https://www.unicode.org/Public/15.0.0/ucd/DerivedCoreProperties.txt</a></p> <section id="keywords"> <span id="id8"></span><h3>
+<span class="section-number">2.3.1. </span>Keywords</h3> <p id="index-13">The following identifiers 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> <pre data-language="text">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> </section> <section id="soft-keywords"> <span id="id9"></span><h3>
+<span class="section-number">2.3.2. </span>Soft Keywords</h3> <div class="versionadded" id="index-14"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> <p>Some identifiers are only reserved under specific contexts. These are known as <em>soft keywords</em>. The identifiers <code>match</code>, <code>case</code>, <code>type</code> and <code>_</code> can syntactically act as keywords in certain 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> <p><code>match</code>, <code>case</code>, and <code>_</code> are used in the <a class="reference internal" href="compound_stmts#match"><code>match</code></a> statement. <code>type</code> is used in the <a class="reference internal" href="simple_stmts#type"><code>type</code></a> statement.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.12: </span><code>type</code> is now a soft keyword.</p> </div> </section> <section id="reserved-classes-of-identifiers"> <span id="id-classes"></span><span id="index-15"></span><h3>
+<span class="section-number">2.3.3. </span>Reserved classes of identifiers</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>_*</code> </dt>
+<dd>
+<p>Not imported by <code>from module import *</code>.</p> </dd> <dt>
+<code>_</code> </dt>
+<dd>
+<p>In a <code>case</code> pattern within a <a class="reference internal" href="compound_stmts#match"><code>match</code></a> statement, <code>_</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#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>_</code>. (It is stored in the <a class="reference internal" href="../library/builtins#module-builtins" title="builtins: The module that provides the built-in namespace."><code>builtins</code></a> module, alongside built-in functions like <code>print</code>.)</p> <p>Elsewhere, <code>_</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>_</code> is often used in conjunction with internationalization; refer to the documentation for the <a class="reference internal" href="../library/gettext#module-gettext" title="gettext: Multilingual internationalization services."><code>gettext</code></a> module for more information on this convention.</p> <p>It is also commonly used for unused variables.</p> </div> </dd> <dt>
+<code>__*__</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#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>__*__</code> names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.</p> </dd> <dt>
+<code>__*</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#atom-identifiers"><span class="std std-ref">Identifiers (Names)</span></a>.</p> </dd> </dl> </section> </section> <section id="literals"> <span id="id10"></span><h2>
+<span class="section-number">2.4. </span>Literals</h2> <p id="index-16">Literals are notations for constant values of some built-in types.</p> <section id="string-and-bytes-literals"> <span id="strings"></span><span id="index-17"></span><h3>
+<span class="section-number">2.4.1. </span>String and Bytes literals</h3> <p>String literals are described by the following lexical definitions:</p> <pre>
+<strong id="grammar-token-python-grammar-stringliteral"><span id="grammar-token-stringliteral"></span>stringliteral </strong> ::= [<a class="reference internal" href="#grammar-token-python-grammar-stringprefix">stringprefix</a>](<a class="reference internal" href="#grammar-token-python-grammar-shortstring">shortstring</a> | <a class="reference internal" href="#grammar-token-python-grammar-longstring">longstring</a>)
+<strong id="grammar-token-python-grammar-stringprefix"><span id="grammar-token-stringprefix"></span>stringprefix </strong> ::= "r" | "u" | "R" | "U" | "f" | "F"
+ | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF"
+<strong id="grammar-token-python-grammar-shortstring"><span id="grammar-token-shortstring"></span>shortstring </strong> ::= "'" <a class="reference internal" href="#grammar-token-python-grammar-shortstringitem">shortstringitem</a>* "'" | '"' <a class="reference internal" href="#grammar-token-python-grammar-shortstringitem">shortstringitem</a>* '"'
+<strong id="grammar-token-python-grammar-longstring"><span id="grammar-token-longstring"></span>longstring </strong> ::= "'''" <a class="reference internal" href="#grammar-token-python-grammar-longstringitem">longstringitem</a>* "'''" | '"""' <a class="reference internal" href="#grammar-token-python-grammar-longstringitem">longstringitem</a>* '"""'
+<strong id="grammar-token-python-grammar-shortstringitem"><span id="grammar-token-shortstringitem"></span>shortstringitem</strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-shortstringchar">shortstringchar</a> | <a class="reference internal" href="#grammar-token-python-grammar-stringescapeseq">stringescapeseq</a>
+<strong id="grammar-token-python-grammar-longstringitem"><span id="grammar-token-longstringitem"></span>longstringitem </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-longstringchar">longstringchar</a> | <a class="reference internal" href="#grammar-token-python-grammar-stringescapeseq">stringescapeseq</a>
+<strong id="grammar-token-python-grammar-shortstringchar"><span id="grammar-token-shortstringchar"></span>shortstringchar</strong> ::= &lt;any source character except "\" or newline or the quote&gt;
+<strong id="grammar-token-python-grammar-longstringchar"><span id="grammar-token-longstringchar"></span>longstringchar </strong> ::= &lt;any source character except "\"&gt;
+<strong id="grammar-token-python-grammar-stringescapeseq"><span id="grammar-token-stringescapeseq"></span>stringescapeseq</strong> ::= "\" &lt;any source character&gt;
+</pre> <pre>
+<strong id="grammar-token-python-grammar-bytesliteral"><span id="grammar-token-bytesliteral"></span>bytesliteral </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-bytesprefix">bytesprefix</a>(<a class="reference internal" href="#grammar-token-python-grammar-shortbytes">shortbytes</a> | <a class="reference internal" href="#grammar-token-python-grammar-longbytes">longbytes</a>)
+<strong id="grammar-token-python-grammar-bytesprefix"><span id="grammar-token-bytesprefix"></span>bytesprefix </strong> ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB"
+<strong id="grammar-token-python-grammar-shortbytes"><span id="grammar-token-shortbytes"></span>shortbytes </strong> ::= "'" <a class="reference internal" href="#grammar-token-python-grammar-shortbytesitem">shortbytesitem</a>* "'" | '"' <a class="reference internal" href="#grammar-token-python-grammar-shortbytesitem">shortbytesitem</a>* '"'
+<strong id="grammar-token-python-grammar-longbytes"><span id="grammar-token-longbytes"></span>longbytes </strong> ::= "'''" <a class="reference internal" href="#grammar-token-python-grammar-longbytesitem">longbytesitem</a>* "'''" | '"""' <a class="reference internal" href="#grammar-token-python-grammar-longbytesitem">longbytesitem</a>* '"""'
+<strong id="grammar-token-python-grammar-shortbytesitem"><span id="grammar-token-shortbytesitem"></span>shortbytesitem</strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-shortbyteschar">shortbyteschar</a> | <a class="reference internal" href="#grammar-token-python-grammar-bytesescapeseq">bytesescapeseq</a>
+<strong id="grammar-token-python-grammar-longbytesitem"><span id="grammar-token-longbytesitem"></span>longbytesitem </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-longbyteschar">longbyteschar</a> | <a class="reference internal" href="#grammar-token-python-grammar-bytesescapeseq">bytesescapeseq</a>
+<strong id="grammar-token-python-grammar-shortbyteschar"><span id="grammar-token-shortbyteschar"></span>shortbyteschar</strong> ::= &lt;any ASCII character except "\" or newline or the quote&gt;
+<strong id="grammar-token-python-grammar-longbyteschar"><span id="grammar-token-longbyteschar"></span>longbyteschar </strong> ::= &lt;any ASCII character except "\"&gt;
+<strong id="grammar-token-python-grammar-bytesescapeseq"><span id="grammar-token-bytesescapeseq"></span>bytesescapeseq</strong> ::= "\" &lt;any ASCII character&gt;
+</pre> <p>One syntactic restriction not indicated by these productions is that whitespace is not allowed between the <a class="reference internal" href="#grammar-token-python-grammar-stringprefix"><code>stringprefix</code></a> or <a class="reference internal" href="#grammar-token-python-grammar-bytesprefix"><code>bytesprefix</code></a> and the rest of the literal. The source character set is defined by the encoding declaration; it is UTF-8 if no encoding declaration is given in the source file; see section <a class="reference internal" href="#encodings"><span class="std std-ref">Encoding declarations</span></a>.</p> <p id="index-18">In plain English: Both types of literals can be enclosed in matching single quotes (<code>'</code>) or double quotes (<code>"</code>). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as <em>triple-quoted strings</em>). The backslash (<code>\</code>) character is used to give special meaning to otherwise ordinary characters like <code>n</code>, which means ‘newline’ when escaped (<code>\n</code>). It can also be used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. See <a class="reference internal" href="#escape-sequences"><span class="std std-ref">escape sequences</span></a> below for examples.</p> <p id="index-19">Bytes literals are always prefixed with <code>'b'</code> or <code>'B'</code>; they produce an instance of the <a class="reference internal" href="../library/stdtypes#bytes" title="bytes"><code>bytes</code></a> type instead of the <a class="reference internal" href="../library/stdtypes#str" title="str"><code>str</code></a> type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.</p> <p id="index-20">Both string and bytes literals may optionally be prefixed with a letter <code>'r'</code> or <code>'R'</code>; such strings are called <em class="dfn">raw strings</em> and treat backslashes as literal characters. As a result, in string literals, <code>'\U'</code> and <code>'\u'</code> escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the <code>'ur'</code> syntax is not supported.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>The <code>'rb'</code> prefix of raw bytes literals has been added as a synonym of <code>'br'</code>.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3: </span>Support for the unicode legacy literal (<code>u'value'</code>) was reintroduced to simplify the maintenance of dual Python 2.x and 3.x codebases. See <span class="target" id="index-21"></span><a class="pep reference external" href="https://peps.python.org/pep-0414/"><strong>PEP 414</strong></a> for more information.</p> </div> <p id="index-22">A string literal with <code>'f'</code> or <code>'F'</code> in its prefix is a <em class="dfn">formatted string literal</em>; see <a class="reference internal" href="#f-strings"><span class="std std-ref">f-strings</span></a>. The <code>'f'</code> may be combined with <code>'r'</code>, but not with <code>'b'</code> or <code>'u'</code>, therefore raw formatted strings are possible, but formatted bytes literals are not.</p> <p>In triple-quoted literals, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the literal. (A “quote” is the character used to open the literal, i.e. either <code>'</code> or <code>"</code>.)</p> <section id="escape-sequences"> <span id="index-23"></span><span id="id11"></span><h4>
+<span class="section-number">2.4.1.1. </span>Escape sequences</h4> <p>Unless an <code>'r'</code> or <code>'R'</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>
+<th class="head"><p>Escape Sequence</p></th> <th class="head"><p>Meaning</p></th> <th class="head"><p>Notes</p></th> </tr> </thead> <tr>
+<td><p><code>\</code>&lt;newline&gt;</p></td> <td><p>Backslash and newline ignored</p></td> <td><p>(1)</p></td> </tr> <tr>
+<td><p><code>\\</code></p></td> <td><p>Backslash (<code>\</code>)</p></td> <td></td> </tr> <tr>
+<td><p><code>\'</code></p></td> <td><p>Single quote (<code>'</code>)</p></td> <td></td> </tr> <tr>
+<td><p><code>\"</code></p></td> <td><p>Double quote (<code>"</code>)</p></td> <td></td> </tr> <tr>
+<td><p><code>\a</code></p></td> <td><p>ASCII Bell (BEL)</p></td> <td></td> </tr> <tr>
+<td><p><code>\b</code></p></td> <td><p>ASCII Backspace (BS)</p></td> <td></td> </tr> <tr>
+<td><p><code>\f</code></p></td> <td><p>ASCII Formfeed (FF)</p></td> <td></td> </tr> <tr>
+<td><p><code>\n</code></p></td> <td><p>ASCII Linefeed (LF)</p></td> <td></td> </tr> <tr>
+<td><p><code>\r</code></p></td> <td><p>ASCII Carriage Return (CR)</p></td> <td></td> </tr> <tr>
+<td><p><code>\t</code></p></td> <td><p>ASCII Horizontal Tab (TAB)</p></td> <td></td> </tr> <tr>
+<td><p><code>\v</code></p></td> <td><p>ASCII Vertical Tab (VT)</p></td> <td></td> </tr> <tr>
+<td><p><code>\<em>ooo</em></code></p></td> <td><p>Character with octal value <em>ooo</em></p></td> <td><p>(2,4)</p></td> </tr> <tr>
+<td><p><code>\x<em>hh</em></code></p></td> <td><p>Character with hex value <em>hh</em></p></td> <td><p>(3,4)</p></td> </tr> </table> <p>Escape sequences only recognized in string literals are:</p> <table class="docutils align-default"> <thead> <tr>
+<th class="head"><p>Escape Sequence</p></th> <th class="head"><p>Meaning</p></th> <th class="head"><p>Notes</p></th> </tr> </thead> <tr>
+<td><p><code>\N{<em>name</em>}</code></p></td> <td><p>Character named <em>name</em> in the Unicode database</p></td> <td><p>(5)</p></td> </tr> <tr>
+<td><p><code>\u<em>xxxx</em></code></p></td> <td><p>Character with 16-bit hex value <em>xxxx</em></p></td> <td><p>(6)</p></td> </tr> <tr>
+<td><p><code>\U<em>xxxxxxxx</em></code></p></td> <td><p>Character with 32-bit hex value <em>xxxxxxxx</em></p></td> <td><p>(7)</p></td> </tr> </table> <p>Notes:</p> <ol class="arabic"> <li>
+<p>A backslash can be added at the end of a line to ignore the newline:</p> <pre data-language="python">&gt;&gt;&gt; 'This string will not include \
+... backslashes or newline characters.'
+'This string will not include backslashes or newline characters.'
+</pre> <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="#string-concatenation"><span class="std std-ref">string literal concatenation</span></a>.</p> </li> <li>
+<p>As in Standard C, up to three octal digits are accepted.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>Octal escapes with value larger than <code>0o377</code> produce a <a class="reference internal" href="../library/exceptions#DeprecationWarning" title="DeprecationWarning"><code>DeprecationWarning</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>0o377</code> produce a <a class="reference internal" href="../library/exceptions#SyntaxWarning" title="SyntaxWarning"><code>SyntaxWarning</code></a>. In a future Python version they will be eventually a <a class="reference internal" href="../library/exceptions#SyntaxError" title="SyntaxError"><code>SyntaxError</code></a>.</p> </div> </li> <li>Unlike in Standard C, exactly two hex digits are required.</li> <li>In a bytes literal, hexadecimal and octal escapes denote the byte with the given value. In a string literal, these escapes denote a Unicode character with the given value.</li> <li>
+<div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>Support for name aliases <a class="footnote-reference brackets" href="#id16" id="id12">1</a> has been added.</p> </div> </li> <li>Exactly four hex digits are required.</li> <li>Any Unicode character can be encoded this way. Exactly eight hex digits are required.</li> </ol> <p id="index-24">Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., <em>the backslash is left in the result</em>. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.) It is also important to note that the escape sequences only recognized in string literals fall into the category of unrecognized escapes for bytes literals.</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#DeprecationWarning" title="DeprecationWarning"><code>DeprecationWarning</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#SyntaxWarning" title="SyntaxWarning"><code>SyntaxWarning</code></a>. In a future Python version they will be eventually a <a class="reference internal" href="../library/exceptions#SyntaxError" title="SyntaxError"><code>SyntaxError</code></a>.</p> </div> <p>Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; for example, <code>r"\""</code> is a valid string literal consisting of two characters: a backslash and a double quote; <code>r"\"</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> <section id="string-literal-concatenation"> <span id="string-concatenation"></span><h3>
+<span class="section-number">2.4.2. </span>String literal concatenation</h3> <p>Multiple adjacent string or bytes literals (delimited by whitespace), possibly using different quoting conventions, are allowed, and their meaning is the same as their concatenation. Thus, <code>"hello" 'world'</code> is equivalent to <code>"helloworld"</code>. This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings, for example:</p> <pre data-language="python">re.compile("[A-Za-z_]" # letter or underscore
+ "[A-Za-z0-9_]*" # letter, digit or underscore
+ )
+</pre> <p>Note that this feature is defined at the syntactical level, but implemented at compile time. The ‘+’ operator must be used to concatenate string expressions at run time. Also note that literal concatenation can use different quoting styles for each component (even mixing raw strings and triple quoted strings), and formatted string literals may be concatenated with plain string literals.</p> </section> <section id="formatted-string-literals"> <span id="f-strings"></span><span id="index-25"></span><span id="id13"></span><h3>
+<span class="section-number">2.4.3. </span>f-strings</h3> <div class="versionadded"> <p><span class="versionmodified added">New 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>'f'</code> or <code>'F'</code>. These strings may contain replacement fields, which are expressions delimited by curly braces <code>{}</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>
+<strong id="grammar-token-python-grammar-f_string"><span id="grammar-token-f-string"></span>f_string </strong> ::= (<a class="reference internal" href="#grammar-token-python-grammar-literal_char">literal_char</a> | "{{" | "}}" | <a class="reference internal" href="#grammar-token-python-grammar-replacement_field">replacement_field</a>)*
+<strong id="grammar-token-python-grammar-replacement_field"><span id="grammar-token-replacement-field"></span>replacement_field</strong> ::= "{" <a class="reference internal" href="#grammar-token-python-grammar-f_expression">f_expression</a> ["="] ["!" <a class="reference internal" href="#grammar-token-python-grammar-conversion">conversion</a>] [":" <a class="reference internal" href="#grammar-token-python-grammar-format_spec">format_spec</a>] "}"
+<strong id="grammar-token-python-grammar-f_expression"><span id="grammar-token-f-expression"></span>f_expression </strong> ::= (<a class="reference internal" href="expressions#grammar-token-python-grammar-conditional_expression">conditional_expression</a> | "*" <a class="reference internal" href="expressions#grammar-token-python-grammar-or_expr">or_expr</a>)
+ ("," <a class="reference internal" href="expressions#grammar-token-python-grammar-conditional_expression">conditional_expression</a> | "," "*" <a class="reference internal" href="expressions#grammar-token-python-grammar-or_expr">or_expr</a>)* [","]
+ | <a class="reference internal" href="expressions#grammar-token-python-grammar-yield_expression">yield_expression</a>
+<strong id="grammar-token-python-grammar-conversion"><span id="grammar-token-conversion"></span>conversion </strong> ::= "s" | "r" | "a"
+<strong id="grammar-token-python-grammar-format_spec"><span id="grammar-token-format-spec"></span>format_spec </strong> ::= (<a class="reference internal" href="#grammar-token-python-grammar-literal_char">literal_char</a> | NULL | <a class="reference internal" href="#grammar-token-python-grammar-replacement_field">replacement_field</a>)*
+<strong id="grammar-token-python-grammar-literal_char"><span id="grammar-token-literal-char"></span>literal_char </strong> ::= &lt;any code point except "{", "}" or NULL&gt;
+</pre> <p>The parts of the string outside curly braces are treated literally, except that any doubled curly braces <code>'{{'</code> or <code>'}}'</code> are replaced with the corresponding single curly brace. A single opening curly bracket <code>'{'</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>'='</code> may be added after the expression. A conversion field, introduced by an exclamation point <code>'!'</code> may follow. A format specifier may also be appended, introduced by a colon <code>':'</code>. A replacement field ends with a closing curly bracket <code>'}'</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#lambda"><code>lambda</code></a> and assignment expressions <code>:=</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>#</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> <pre data-language="text">&gt;&gt;&gt; f"abc{a # This is a comment }"
+... + 3}"
+'abc5'
+</pre> <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#await"><code>await</code></a> expression and comprehensions containing an <a class="reference internal" href="compound_stmts#async-for"><code>async for</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>'='</code> is provided, the output will have the expression text, the <code>'='</code> and the evaluated value. Spaces after the opening brace <code>'{'</code>, within the expression and after the <code>'='</code> are all retained in the output. By default, the <code>'='</code> causes the <a class="reference internal" href="../library/functions#repr" title="repr"><code>repr()</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#str" title="str"><code>str()</code></a> of the expression unless a conversion <code>'!r'</code> is declared.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8: </span>The equal sign <code>'='</code>.</p> </div> <p>If a conversion is specified, the result of evaluating the expression is converted before formatting. Conversion <code>'!s'</code> calls <a class="reference internal" href="../library/stdtypes#str" title="str"><code>str()</code></a> on the result, <code>'!r'</code> calls <a class="reference internal" href="../library/functions#repr" title="repr"><code>repr()</code></a>, and <code>'!a'</code> calls <a class="reference internal" href="../library/functions#ascii" title="ascii"><code>ascii()</code></a>.</p> <p>The result is then formatted using the <a class="reference internal" href="../library/functions#format" title="format"><code>format()</code></a> protocol. The format specifier is passed to the <a class="reference internal" href="datamodel#object.__format__" title="object.__format__"><code>__format__()</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#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#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#str.format" title="str.format"><code>str.format()</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> <pre data-language="python">&gt;&gt;&gt; name = "Fred"
+&gt;&gt;&gt; f"He said his name is {name!r}."
+"He said his name is 'Fred'."
+&gt;&gt;&gt; f"He said his name is {repr(name)}." # repr() is equivalent to !r
+"He said his name is 'Fred'."
+&gt;&gt;&gt; width = 10
+&gt;&gt;&gt; precision = 4
+&gt;&gt;&gt; value = decimal.Decimal("12.34567")
+&gt;&gt;&gt; f"result: {value:{width}.{precision}}" # nested fields
+'result: 12.35'
+&gt;&gt;&gt; today = datetime(year=2017, month=1, day=27)
+&gt;&gt;&gt; f"{today:%B %d, %Y}" # using date format specifier
+'January 27, 2017'
+&gt;&gt;&gt; f"{today=:%B %d, %Y}" # using date format specifier and debugging
+'today=January 27, 2017'
+&gt;&gt;&gt; number = 1024
+&gt;&gt;&gt; f"{number:#0x}" # using integer format specifier
+'0x400'
+&gt;&gt;&gt; foo = "bar"
+&gt;&gt;&gt; f"{ foo = }" # preserves whitespace
+" foo = 'bar'"
+&gt;&gt;&gt; line = "The mill's closed"
+&gt;&gt;&gt; f"{line = }"
+'line = "The mill\'s closed"'
+&gt;&gt;&gt; f"{line = :20}"
+"line = The mill's closed "
+&gt;&gt;&gt; f"{line = !r:20}"
+'line = "The mill\'s closed" '
+</pre> <p>Reusing the outer f-string quoting type inside a replacement field is permitted:</p> <pre data-language="python">&gt;&gt;&gt; a = dict(x=2)
+&gt;&gt;&gt; f"abc {a["x"]} def"
+'abc 2 def'
+</pre> <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> <pre data-language="python">&gt;&gt;&gt; a = ["a", "b", "c"]
+&gt;&gt;&gt; print(f"List a contains:\n{"\n".join(a)}")
+List a contains:
+a
+b
+c
+</pre> <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> <pre data-language="python">&gt;&gt;&gt; def foo():
+... f"Not a docstring"
+...
+&gt;&gt;&gt; foo.__doc__ is None
+True
+</pre> <p>See also <span class="target" id="index-26"></span><a class="pep reference external" href="https://peps.python.org/pep-0498/"><strong>PEP 498</strong></a> for the proposal that added formatted string literals, and <a class="reference internal" href="../library/stdtypes#str.format" title="str.format"><code>str.format()</code></a>, which uses a related format string mechanism.</p> </section> <section id="numeric-literals"> <span id="numbers"></span><h3>
+<span class="section-number">2.4.4. </span>Numeric literals</h3> <p id="index-27">There are three types of numeric literals: integers, floating point numbers, and imaginary numbers. There are no complex literals (complex numbers can be formed by adding a real number and an imaginary number).</p> <p>Note that numeric literals do not include a sign; a phrase like <code>-1</code> is actually an expression composed of the unary operator ‘<code>-</code>’ and the literal <code>1</code>.</p> </section> <section id="integer-literals"> <span id="integers"></span><span id="index-28"></span><h3>
+<span class="section-number">2.4.5. </span>Integer literals</h3> <p>Integer literals are described by the following lexical definitions:</p> <pre>
+<strong id="grammar-token-python-grammar-integer"><span id="grammar-token-integer"></span>integer </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-decinteger">decinteger</a> | <a class="reference internal" href="#grammar-token-python-grammar-bininteger">bininteger</a> | <a class="reference internal" href="#grammar-token-python-grammar-octinteger">octinteger</a> | <a class="reference internal" href="#grammar-token-python-grammar-hexinteger">hexinteger</a>
+<strong id="grammar-token-python-grammar-decinteger"><span id="grammar-token-decinteger"></span>decinteger </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-nonzerodigit">nonzerodigit</a> (["_"] <a class="reference internal" href="#grammar-token-python-grammar-digit">digit</a>)* | "0"+ (["_"] "0")*
+<strong id="grammar-token-python-grammar-bininteger"><span id="grammar-token-bininteger"></span>bininteger </strong> ::= "0" ("b" | "B") (["_"] <a class="reference internal" href="#grammar-token-python-grammar-bindigit">bindigit</a>)+
+<strong id="grammar-token-python-grammar-octinteger"><span id="grammar-token-octinteger"></span>octinteger </strong> ::= "0" ("o" | "O") (["_"] <a class="reference internal" href="#grammar-token-python-grammar-octdigit">octdigit</a>)+
+<strong id="grammar-token-python-grammar-hexinteger"><span id="grammar-token-hexinteger"></span>hexinteger </strong> ::= "0" ("x" | "X") (["_"] <a class="reference internal" href="#grammar-token-python-grammar-hexdigit">hexdigit</a>)+
+<strong id="grammar-token-python-grammar-nonzerodigit"><span id="grammar-token-nonzerodigit"></span>nonzerodigit</strong> ::= "1"..."9"
+<strong id="grammar-token-python-grammar-digit"><span id="grammar-token-digit"></span>digit </strong> ::= "0"..."9"
+<strong id="grammar-token-python-grammar-bindigit"><span id="grammar-token-bindigit"></span>bindigit </strong> ::= "0" | "1"
+<strong id="grammar-token-python-grammar-octdigit"><span id="grammar-token-octdigit"></span>octdigit </strong> ::= "0"..."7"
+<strong id="grammar-token-python-grammar-hexdigit"><span id="grammar-token-hexdigit"></span>hexdigit </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-digit">digit</a> | "a"..."f" | "A"..."F"
+</pre> <p>There is no limit for the length of integer literals apart from what can be stored in available memory.</p> <p>Underscores are ignored for determining the numeric value of the literal. They can be used to group digits for enhanced readability. One underscore can occur between digits, and after base specifiers like <code>0x</code>.</p> <p>Note that leading zeros in a non-zero decimal number are not allowed. This is for disambiguation with C-style octal literals, which Python used before version 3.0.</p> <p>Some examples of integer literals:</p> <pre data-language="python">7 2147483647 0o177 0b100110111
+3 79228162514264337593543950336 0o377 0xdeadbeef
+ 100_000_000_000 0b_1110_0101
+</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-29"></span><h3>
+<span class="section-number">2.4.6. </span>Floating point literals</h3> <p>Floating point literals are described by the following lexical definitions:</p> <pre>
+<strong id="grammar-token-python-grammar-floatnumber"><span id="grammar-token-floatnumber"></span>floatnumber </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-pointfloat">pointfloat</a> | <a class="reference internal" href="#grammar-token-python-grammar-exponentfloat">exponentfloat</a>
+<strong id="grammar-token-python-grammar-pointfloat"><span id="grammar-token-pointfloat"></span>pointfloat </strong> ::= [<a class="reference internal" href="#grammar-token-python-grammar-digitpart">digitpart</a>] <a class="reference internal" href="#grammar-token-python-grammar-fraction">fraction</a> | <a class="reference internal" href="#grammar-token-python-grammar-digitpart">digitpart</a> "."
+<strong id="grammar-token-python-grammar-exponentfloat"><span id="grammar-token-exponentfloat"></span>exponentfloat</strong> ::= (<a class="reference internal" href="#grammar-token-python-grammar-digitpart">digitpart</a> | <a class="reference internal" href="#grammar-token-python-grammar-pointfloat">pointfloat</a>) <a class="reference internal" href="#grammar-token-python-grammar-exponent">exponent</a>
+<strong id="grammar-token-python-grammar-digitpart"><span id="grammar-token-digitpart"></span>digitpart </strong> ::= <a class="reference internal" href="#grammar-token-python-grammar-digit">digit</a> (["_"] <a class="reference internal" href="#grammar-token-python-grammar-digit">digit</a>)*
+<strong id="grammar-token-python-grammar-fraction"><span id="grammar-token-fraction"></span>fraction </strong> ::= "." <a class="reference internal" href="#grammar-token-python-grammar-digitpart">digitpart</a>
+<strong id="grammar-token-python-grammar-exponent"><span id="grammar-token-exponent"></span>exponent </strong> ::= ("e" | "E") ["+" | "-"] <a class="reference internal" href="#grammar-token-python-grammar-digitpart">digitpart</a>
+</pre> <p>Note that the integer and exponent parts are always interpreted using radix 10. For example, <code>077e010</code> is legal, and denotes the same number as <code>77e10</code>. The allowed range of floating point literals is implementation-dependent. As in integer literals, underscores are supported for digit grouping.</p> <p>Some examples of floating point literals:</p> <pre data-language="python">3.14 10. .001 1e100 3.14e-10 0e0 3.14_15_93
+</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-30"></span><h3>
+<span class="section-number">2.4.7. </span>Imaginary literals</h3> <p>Imaginary literals are described by the following lexical definitions:</p> <pre>
+<strong id="grammar-token-python-grammar-imagnumber"><span id="grammar-token-imagnumber"></span>imagnumber</strong> ::= (<a class="reference internal" href="#grammar-token-python-grammar-floatnumber">floatnumber</a> | <a class="reference internal" href="#grammar-token-python-grammar-digitpart">digitpart</a>) ("j" | "J")
+</pre> <p>An imaginary literal yields a complex number with a real part of 0.0. Complex numbers are represented as a pair of floating point numbers and have the same restrictions on their range. To create a complex number with a nonzero real part, add a floating point number to it, e.g., <code>(3+4j)</code>. Some examples of imaginary literals:</p> <pre data-language="python">3.14j 10.j 10j .001j 1e100j 3.14e-10j 3.14_15_93j
+</pre> </section> </section> <section id="operators"> <span id="id14"></span><h2>
+<span class="section-number">2.5. </span>Operators</h2> <p id="index-31">The following tokens are operators:</p> <pre data-language="none">+ - * ** / // % @
+&lt;&lt; &gt;&gt; &amp; | ^ ~ :=
+&lt; &gt; &lt;= &gt;= == !=
+</pre> </section> <section id="delimiters"> <span id="id15"></span><h2>
+<span class="section-number">2.6. </span>Delimiters</h2> <p id="index-32">The following tokens serve as delimiters in the grammar:</p> <pre data-language="none">( ) [ ] { }
+, : . ; @ = -&gt;
++= -= *= /= //= %= @=
+&amp;= |= ^= &gt;&gt;= &lt;&lt;= **=
+</pre> <p>The period can also occur in floating-point and imaginary literals. A sequence of three periods has a special meaning as an ellipsis literal. The second half of the list, the augmented assignment operators, serve lexically as delimiters, but also perform an operation.</p> <p>The following printing ASCII characters have special meaning as part of other tokens or are otherwise significant to the lexical analyzer:</p> <pre data-language="none">' " # \
+</pre> <p>The following printing ASCII characters are not used in Python. Their occurrence outside string literals and comments is an unconditional error:</p> <pre data-language="none">$ ? `
+</pre> <h4 class="rubric">Footnotes</h4> <dl class="footnote brackets"> <dt class="label" id="id16">
+<code>1</code> </dt> <dd>
+<p><a class="reference external" href="https://www.unicode.org/Public/15.0.0/ucd/NameAliases.txt">https://www.unicode.org/Public/15.0.0/ucd/NameAliases.txt</a></p> </dd> </dl> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/reference/lexical_analysis.html" class="_attribution-link">https://docs.python.org/3.12/reference/lexical_analysis.html</a>
+ </p>
+</div>