blob: fdb5f99bdd6e7e7d13606f176f93076e71ead3d6 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2.. _lexical:
3
4****************
5Lexical analysis
6****************
7
Georg Brandl57e3b682007-08-31 08:07:45 +00008.. index:: lexical analysis, parser, token
Georg Brandl116aa622007-08-15 14:28:22 +00009
10A Python program is read by a *parser*. Input to the parser is a stream of
11*tokens*, generated by the *lexical analyzer*. This chapter describes how the
12lexical analyzer breaks a file into tokens.
13
Georg Brandl57e3b682007-08-31 08:07:45 +000014Python reads program text as Unicode code points; the encoding of a source file
15can be given by an encoding declaration and defaults to UTF-8, see :pep:`3120`
16for details. If the source file cannot be decoded, a :exc:`SyntaxError` is
17raised.
Georg Brandl116aa622007-08-15 14:28:22 +000018
19
20.. _line-structure:
21
22Line structure
23==============
24
Georg Brandl57e3b682007-08-31 08:07:45 +000025.. index:: line structure
Georg Brandl116aa622007-08-15 14:28:22 +000026
27A Python program is divided into a number of *logical lines*.
28
29
Georg Brandl57e3b682007-08-31 08:07:45 +000030.. _logical-lines:
Georg Brandl116aa622007-08-15 14:28:22 +000031
32Logical lines
33-------------
34
Georg Brandl57e3b682007-08-31 08:07:45 +000035.. index:: logical line, physical line, line joining, NEWLINE token
Georg Brandl116aa622007-08-15 14:28:22 +000036
37The end of a logical line is represented by the token NEWLINE. Statements
38cannot cross logical line boundaries except where NEWLINE is allowed by the
39syntax (e.g., between statements in compound statements). A logical line is
40constructed from one or more *physical lines* by following the explicit or
41implicit *line joining* rules.
42
43
Georg Brandl57e3b682007-08-31 08:07:45 +000044.. _physical-lines:
Georg Brandl116aa622007-08-15 14:28:22 +000045
46Physical lines
47--------------
48
49A physical line is a sequence of characters terminated by an end-of-line
50sequence. In source files, any of the standard platform line termination
51sequences can be used - the Unix form using ASCII LF (linefeed), the Windows
52form using the ASCII sequence CR LF (return followed by linefeed), or the
53Macintosh form using the ASCII CR (return) character. All of these forms can be
54used equally, regardless of platform.
55
56When embedding Python, source code strings should be passed to Python APIs using
57the standard C conventions for newline characters (the ``\n`` character,
58representing ASCII LF, is the line terminator).
59
60
61.. _comments:
62
63Comments
64--------
65
Georg Brandl57e3b682007-08-31 08:07:45 +000066.. index:: comment, hash character
Georg Brandl116aa622007-08-15 14:28:22 +000067
68A comment starts with a hash character (``#``) that is not part of a string
69literal, and ends at the end of the physical line. A comment signifies the end
70of the logical line unless the implicit line joining rules are invoked. Comments
71are ignored by the syntax; they are not tokens.
72
73
74.. _encodings:
75
76Encoding declarations
77---------------------
78
Georg Brandl57e3b682007-08-31 08:07:45 +000079.. index:: source character set, encodings
Georg Brandl116aa622007-08-15 14:28:22 +000080
81If a comment in the first or second line of the Python script matches the
82regular expression ``coding[=:]\s*([-\w.]+)``, this comment is processed as an
83encoding declaration; the first group of this expression names the encoding of
84the source code file. The recommended forms of this expression are ::
85
86 # -*- coding: <encoding-name> -*-
87
88which is recognized also by GNU Emacs, and ::
89
90 # vim:fileencoding=<encoding-name>
91
Georg Brandl57e3b682007-08-31 08:07:45 +000092which is recognized by Bram Moolenaar's VIM.
93
94If no encoding declaration is found, the default encoding is UTF-8. In
95addition, if the first bytes of the file are the UTF-8 byte-order mark
96(``b'\xef\xbb\xbf'``), the declared file encoding is UTF-8 (this is supported,
97among others, by Microsoft's :program:`notepad`).
Georg Brandl116aa622007-08-15 14:28:22 +000098
99If an encoding is declared, the encoding name must be recognized by Python. The
Georg Brandl57e3b682007-08-31 08:07:45 +0000100encoding is used for all lexical analysis, including string literals, comments
101and identifiers. The encoding declaration must appear on a line of its own.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000103.. XXX there should be a list of supported encodings.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105
106.. _explicit-joining:
107
108Explicit line joining
109---------------------
110
Georg Brandl57e3b682007-08-31 08:07:45 +0000111.. index:: physical line, line joining, line continuation, backslash character
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113Two or more physical lines may be joined into logical lines using backslash
114characters (``\``), as follows: when a physical line ends in a backslash that is
115not part of a string literal or comment, it is joined with the following forming
116a single logical line, deleting the backslash and the following end-of-line
Georg Brandl57e3b682007-08-31 08:07:45 +0000117character. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119 if 1900 < year < 2100 and 1 <= month <= 12 \
120 and 1 <= day <= 31 and 0 <= hour < 24 \
121 and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
122 return 1
123
124A line ending in a backslash cannot carry a comment. A backslash does not
125continue a comment. A backslash does not continue a token except for string
126literals (i.e., tokens other than string literals cannot be split across
127physical lines using a backslash). A backslash is illegal elsewhere on a line
128outside a string literal.
129
130
131.. _implicit-joining:
132
133Implicit line joining
134---------------------
135
136Expressions in parentheses, square brackets or curly braces can be split over
137more than one physical line without using backslashes. For example::
138
139 month_names = ['Januari', 'Februari', 'Maart', # These are the
140 'April', 'Mei', 'Juni', # Dutch names
141 'Juli', 'Augustus', 'September', # for the months
142 'Oktober', 'November', 'December'] # of the year
143
144Implicitly continued lines can carry comments. The indentation of the
145continuation lines is not important. Blank continuation lines are allowed.
146There is no NEWLINE token between implicit continuation lines. Implicitly
147continued lines can also occur within triple-quoted strings (see below); in that
148case they cannot carry comments.
149
150
151.. _blank-lines:
152
153Blank lines
154-----------
155
156.. index:: single: blank line
157
158A logical line that contains only spaces, tabs, formfeeds and possibly a
159comment, is ignored (i.e., no NEWLINE token is generated). During interactive
160input of statements, handling of a blank line may differ depending on the
Georg Brandl57e3b682007-08-31 08:07:45 +0000161implementation of the read-eval-print loop. In the standard interactive
162interpreter, an entirely blank logical line (i.e. one containing not even
163whitespace or a comment) terminates a multi-line statement.
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165
166.. _indentation:
167
168Indentation
169-----------
170
Georg Brandl57e3b682007-08-31 08:07:45 +0000171.. index:: indentation, leading whitespace, space, tab, grouping, statement grouping
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173Leading whitespace (spaces and tabs) at the beginning of a logical line is used
174to compute the indentation level of the line, which in turn is used to determine
175the grouping of statements.
176
177First, tabs are replaced (from left to right) by one to eight spaces such that
178the total number of characters up to and including the replacement is a multiple
179of eight (this is intended to be the same rule as used by Unix). The total
180number of spaces preceding the first non-blank character then determines the
181line's indentation. Indentation cannot be split over multiple physical lines
182using backslashes; the whitespace up to the first backslash determines the
183indentation.
184
185**Cross-platform compatibility note:** because of the nature of text editors on
186non-UNIX platforms, it is unwise to use a mixture of spaces and tabs for the
187indentation in a single source file. It should also be noted that different
188platforms may explicitly limit the maximum indentation level.
189
190A formfeed character may be present at the start of the line; it will be ignored
191for the indentation calculations above. Formfeed characters occurring elsewhere
192in the leading whitespace have an undefined effect (for instance, they may reset
193the space count to zero).
194
Georg Brandl57e3b682007-08-31 08:07:45 +0000195.. index:: INDENT token, DEDENT token
Georg Brandl116aa622007-08-15 14:28:22 +0000196
197The indentation levels of consecutive lines are used to generate INDENT and
198DEDENT tokens, using a stack, as follows.
199
200Before the first line of the file is read, a single zero is pushed on the stack;
201this will never be popped off again. The numbers pushed on the stack will
202always be strictly increasing from bottom to top. At the beginning of each
203logical line, the line's indentation level is compared to the top of the stack.
204If it is equal, nothing happens. If it is larger, it is pushed on the stack, and
205one INDENT token is generated. If it is smaller, it *must* be one of the
206numbers occurring on the stack; all numbers on the stack that are larger are
207popped off, and for each number popped off a DEDENT token is generated. At the
208end of the file, a DEDENT token is generated for each number remaining on the
209stack that is larger than zero.
210
211Here is an example of a correctly (though confusingly) indented piece of Python
212code::
213
214 def perm(l):
215 # Compute the list of all permutations of l
216 if len(l) <= 1:
217 return [l]
218 r = []
219 for i in range(len(l)):
220 s = l[:i] + l[i+1:]
221 p = perm(s)
222 for x in p:
223 r.append(l[i:i+1] + x)
224 return r
225
226The following example shows various indentation errors::
227
228 def perm(l): # error: first line indented
229 for i in range(len(l)): # error: not indented
230 s = l[:i] + l[i+1:]
231 p = perm(l[:i] + l[i+1:]) # error: unexpected indent
232 for x in p:
233 r.append(l[i:i+1] + x)
234 return r # error: inconsistent dedent
235
236(Actually, the first three errors are detected by the parser; only the last
237error is found by the lexical analyzer --- the indentation of ``return r`` does
238not match a level popped off the stack.)
239
240
241.. _whitespace:
242
243Whitespace between tokens
244-------------------------
245
246Except at the beginning of a logical line or in string literals, the whitespace
247characters space, tab and formfeed can be used interchangeably to separate
248tokens. Whitespace is needed between two tokens only if their concatenation
249could otherwise be interpreted as a different token (e.g., ab is one token, but
250a b is two tokens).
251
252
253.. _other-tokens:
254
255Other tokens
256============
257
258Besides NEWLINE, INDENT and DEDENT, the following categories of tokens exist:
259*identifiers*, *keywords*, *literals*, *operators*, and *delimiters*. Whitespace
260characters (other than line terminators, discussed earlier) are not tokens, but
261serve to delimit tokens. Where ambiguity exists, a token comprises the longest
262possible string that forms a legal token, when read from left to right.
263
264
265.. _identifiers:
266
267Identifiers and keywords
268========================
269
Georg Brandl57e3b682007-08-31 08:07:45 +0000270.. index:: identifier, name
Georg Brandl116aa622007-08-15 14:28:22 +0000271
272Identifiers (also referred to as *names*) are described by the following lexical
Georg Brandle06de8b2008-05-05 21:42:51 +0000273definitions.
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Georg Brandl57e3b682007-08-31 08:07:45 +0000275The syntax of identifiers in Python is based on the Unicode standard annex
Georg Brandle06de8b2008-05-05 21:42:51 +0000276UAX-31, with elaboration and changes as defined below; see also :pep:`3131` for
277further details.
Georg Brandl57e3b682007-08-31 08:07:45 +0000278
279Within the ASCII range (U+0001..U+007F), the valid characters for identifiers
Georg Brandle06de8b2008-05-05 21:42:51 +0000280are the same as in Python 2.x: the uppercase and lowercase letters ``A`` through
281``Z``, the underscore ``_`` and, except for the first character, the digits
282``0`` through ``9``.
283
284Python 3.0 introduces additional characters from outside the ASCII range (see
285:pep:`3131`). For these characters, the classification uses the version of the
286Unicode Character Database as included in the :mod:`unicodedata` module.
Georg Brandl116aa622007-08-15 14:28:22 +0000287
288Identifiers are unlimited in length. Case is significant.
289
Georg Brandl57e3b682007-08-31 08:07:45 +0000290.. productionlist::
291 identifier: `id_start` `id_continue`*
Mark Summerfield051d1dd2007-11-20 13:22:19 +0000292 id_start: <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
293 id_continue: <all characters in `id_start`, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>
Georg Brandl57e3b682007-08-31 08:07:45 +0000294
295The Unicode category codes mentioned above stand for:
296
297* *Lu* - uppercase letters
298* *Ll* - lowercase letters
299* *Lt* - titlecase letters
300* *Lm* - modifier letters
301* *Lo* - other letters
302* *Nl* - letter numbers
303* *Mn* - nonspacing marks
304* *Mc* - spacing combining marks
305* *Nd* - decimal numbers
306* *Pc* - connector punctuations
307
308All identifiers are converted into the normal form NFC while parsing; comparison
309of identifiers is based on NFC.
310
311A non-normative HTML file listing all valid identifier characters for Unicode
3124.1 can be found at
313http://www.dcl.hpi.uni-potsdam.de/home/loewis/table-3131.html.
Georg Brandl116aa622007-08-15 14:28:22 +0000314
Mark Summerfield051d1dd2007-11-20 13:22:19 +0000315
Georg Brandl116aa622007-08-15 14:28:22 +0000316.. _keywords:
317
318Keywords
319--------
320
321.. index::
322 single: keyword
323 single: reserved word
324
325The following identifiers are used as reserved words, or *keywords* of the
326language, and cannot be used as ordinary identifiers. They must be spelled
327exactly as written here::
328
Georg Brandl57e3b682007-08-31 08:07:45 +0000329 False class finally is return
330 None continue for lambda try
331 True def from nonlocal while
332 and del global not with
333 as elif if or yield
334 assert else import pass
335 break except in raise
Georg Brandl116aa622007-08-15 14:28:22 +0000336
337.. _id-classes:
338
339Reserved classes of identifiers
340-------------------------------
341
342Certain classes of identifiers (besides keywords) have special meanings. These
343classes are identified by the patterns of leading and trailing underscore
344characters:
345
346``_*``
347 Not imported by ``from module import *``. The special identifier ``_`` is used
348 in the interactive interpreter to store the result of the last evaluation; it is
Georg Brandl1a3284e2007-12-02 09:40:06 +0000349 stored in the :mod:`builtins` module. When not in interactive mode, ``_``
Georg Brandl116aa622007-08-15 14:28:22 +0000350 has no special meaning and is not defined. See section :ref:`import`.
351
352 .. note::
353
354 The name ``_`` is often used in conjunction with internationalization;
355 refer to the documentation for the :mod:`gettext` module for more
356 information on this convention.
357
358``__*__``
359 System-defined names. These names are defined by the interpreter and its
360 implementation (including the standard library); applications should not expect
361 to define additional names using this convention. The set of names of this
362 class defined by Python may be extended in future versions. See section
363 :ref:`specialnames`.
364
365``__*``
366 Class-private names. Names in this category, when used within the context of a
367 class definition, are re-written to use a mangled form to help avoid name
368 clashes between "private" attributes of base and derived classes. See section
369 :ref:`atom-identifiers`.
370
371
372.. _literals:
373
374Literals
375========
376
Georg Brandl57e3b682007-08-31 08:07:45 +0000377.. index:: literal, constant
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379Literals are notations for constant values of some built-in types.
380
381
382.. _strings:
383
Georg Brandl57e3b682007-08-31 08:07:45 +0000384String and Bytes literals
385-------------------------
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Georg Brandl57e3b682007-08-31 08:07:45 +0000387.. index:: string literal, bytes literal, ASCII
Georg Brandl116aa622007-08-15 14:28:22 +0000388
389String literals are described by the following lexical definitions:
390
Georg Brandl116aa622007-08-15 14:28:22 +0000391.. productionlist::
392 stringliteral: [`stringprefix`](`shortstring` | `longstring`)
Georg Brandl57e3b682007-08-31 08:07:45 +0000393 stringprefix: "r" | "R"
Georg Brandl116aa622007-08-15 14:28:22 +0000394 shortstring: "'" `shortstringitem`* "'" | '"' `shortstringitem`* '"'
Georg Brandl57e3b682007-08-31 08:07:45 +0000395 longstring: "'''" `longstringitem`* "'''" | '"""' `longstringitem`* '"""'
396 shortstringitem: `shortstringchar` | `stringescapeseq`
397 longstringitem: `longstringchar` | `stringescapeseq`
Georg Brandl116aa622007-08-15 14:28:22 +0000398 shortstringchar: <any source character except "\" or newline or the quote>
399 longstringchar: <any source character except "\">
Georg Brandl57e3b682007-08-31 08:07:45 +0000400 stringescapeseq: "\" <any source character>
401
402.. productionlist::
403 bytesliteral: `bytesprefix`(`shortbytes` | `longbytes`)
404 bytesprefix: "b" | "B"
405 shortbytes: "'" `shortbytesitem`* "'" | '"' `shortbytesitem`* '"'
406 longbytes: "'''" `longbytesitem`* "'''" | '"""' `longbytesitem`* '"""'
407 shortbytesitem: `shortbyteschar` | `bytesescapeseq`
408 longbytesitem: `longbyteschar` | `bytesescapeseq`
409 shortbyteschar: <any ASCII character except "\" or newline or the quote>
410 longbyteschar: <any ASCII character except "\">
411 bytesescapeseq: "\" <any ASCII character>
Georg Brandl116aa622007-08-15 14:28:22 +0000412
413One syntactic restriction not indicated by these productions is that whitespace
Georg Brandl57e3b682007-08-31 08:07:45 +0000414is not allowed between the :token:`stringprefix` or :token:`bytesprefix` and the
415rest of the literal. The source character set is defined by the encoding
416declaration; it is UTF-8 if no encoding declaration is given in the source file;
417see section :ref:`encodings`.
Georg Brandl116aa622007-08-15 14:28:22 +0000418
Georg Brandl57e3b682007-08-31 08:07:45 +0000419.. index:: triple-quoted string, Unicode Consortium, raw string
Georg Brandl116aa622007-08-15 14:28:22 +0000420
Georg Brandl57e3b682007-08-31 08:07:45 +0000421In plain English: Both types of literals can be enclosed in matching single quotes
Georg Brandl116aa622007-08-15 14:28:22 +0000422(``'``) or double quotes (``"``). They can also be enclosed in matching groups
423of three single or double quotes (these are generally referred to as
424*triple-quoted strings*). The backslash (``\``) character is used to escape
425characters that otherwise have a special meaning, such as newline, backslash
Georg Brandl57e3b682007-08-31 08:07:45 +0000426itself, or the quote character.
427
428String literals may optionally be prefixed with a letter ``'r'`` or ``'R'``;
Benjamin Petersona2f837f2008-04-28 21:05:10 +0000429such strings are called :dfn:`raw strings` and treat backslashes as literal
430characters. As a result, ``'\U'`` and ``'\u'`` escapes in raw strings are not
431treated specially.
Georg Brandl57e3b682007-08-31 08:07:45 +0000432
433Bytes literals are always prefixed with ``'b'`` or ``'B'``; they produce an
434instance of the :class:`bytes` type instead of the :class:`str` type. They
435may only contain ASCII characters; bytes with a numeric value of 128 or greater
436must be expressed with escapes.
Georg Brandl116aa622007-08-15 14:28:22 +0000437
438In triple-quoted strings, unescaped newlines and quotes are allowed (and are
439retained), except that three unescaped quotes in a row terminate the string. (A
440"quote" is the character used to open the string, i.e. either ``'`` or ``"``.)
441
Georg Brandl57e3b682007-08-31 08:07:45 +0000442.. index:: physical line, escape sequence, Standard C, C
Georg Brandl116aa622007-08-15 14:28:22 +0000443
444Unless an ``'r'`` or ``'R'`` prefix is present, escape sequences in strings are
445interpreted according to rules similar to those used by Standard C. The
446recognized escape sequences are:
447
448+-----------------+---------------------------------+-------+
449| Escape Sequence | Meaning | Notes |
450+=================+=================================+=======+
Georg Brandl57e3b682007-08-31 08:07:45 +0000451| ``\newline`` | Backslash and newline ignored | |
Georg Brandl116aa622007-08-15 14:28:22 +0000452+-----------------+---------------------------------+-------+
453| ``\\`` | Backslash (``\``) | |
454+-----------------+---------------------------------+-------+
455| ``\'`` | Single quote (``'``) | |
456+-----------------+---------------------------------+-------+
457| ``\"`` | Double quote (``"``) | |
458+-----------------+---------------------------------+-------+
459| ``\a`` | ASCII Bell (BEL) | |
460+-----------------+---------------------------------+-------+
461| ``\b`` | ASCII Backspace (BS) | |
462+-----------------+---------------------------------+-------+
463| ``\f`` | ASCII Formfeed (FF) | |
464+-----------------+---------------------------------+-------+
465| ``\n`` | ASCII Linefeed (LF) | |
466+-----------------+---------------------------------+-------+
Georg Brandl116aa622007-08-15 14:28:22 +0000467| ``\r`` | ASCII Carriage Return (CR) | |
468+-----------------+---------------------------------+-------+
469| ``\t`` | ASCII Horizontal Tab (TAB) | |
470+-----------------+---------------------------------+-------+
Georg Brandl116aa622007-08-15 14:28:22 +0000471| ``\v`` | ASCII Vertical Tab (VT) | |
472+-----------------+---------------------------------+-------+
Georg Brandl57e3b682007-08-31 08:07:45 +0000473| ``\ooo`` | Character with octal value | (1,3) |
Georg Brandl116aa622007-08-15 14:28:22 +0000474| | *ooo* | |
475+-----------------+---------------------------------+-------+
Georg Brandl57e3b682007-08-31 08:07:45 +0000476| ``\xhh`` | Character with hex value *hh* | (2,3) |
Georg Brandl116aa622007-08-15 14:28:22 +0000477+-----------------+---------------------------------+-------+
478
Georg Brandl57e3b682007-08-31 08:07:45 +0000479Escape sequences only recognized in string literals are:
480
481+-----------------+---------------------------------+-------+
482| Escape Sequence | Meaning | Notes |
483+=================+=================================+=======+
484| ``\N{name}`` | Character named *name* in the | |
485| | Unicode database | |
486+-----------------+---------------------------------+-------+
487| ``\uxxxx`` | Character with 16-bit hex value | \(4) |
488| | *xxxx* | |
489+-----------------+---------------------------------+-------+
490| ``\Uxxxxxxxx`` | Character with 32-bit hex value | \(5) |
491| | *xxxxxxxx* | |
492+-----------------+---------------------------------+-------+
Georg Brandl116aa622007-08-15 14:28:22 +0000493
494Notes:
495
496(1)
Georg Brandl57e3b682007-08-31 08:07:45 +0000497 As in Standard C, up to three octal digits are accepted.
498
499(2)
500 Unlike in Standard C, at most two hex digits are accepted.
501
502(3)
503 In a bytes literal, hexadecimal and octal escapes denote the byte with the
504 given value. In a string literal, these escapes denote a Unicode character
505 with the given value.
506
507(4)
Georg Brandl116aa622007-08-15 14:28:22 +0000508 Individual code units which form parts of a surrogate pair can be encoded using
Christian Heimes1af737c2008-01-23 08:24:23 +0000509 this escape sequence. Unlike in Standard C, exactly two hex digits are required.
Georg Brandl116aa622007-08-15 14:28:22 +0000510
Georg Brandl57e3b682007-08-31 08:07:45 +0000511(5)
Georg Brandl116aa622007-08-15 14:28:22 +0000512 Any Unicode character can be encoded this way, but characters outside the Basic
513 Multilingual Plane (BMP) will be encoded using a surrogate pair if Python is
514 compiled to use 16-bit code units (the default). Individual code units which
515 form parts of a surrogate pair can be encoded using this escape sequence.
516
Georg Brandl116aa622007-08-15 14:28:22 +0000517
Georg Brandl57e3b682007-08-31 08:07:45 +0000518.. index:: unrecognized escape sequence
Georg Brandl116aa622007-08-15 14:28:22 +0000519
520Unlike Standard C, all unrecognized escape sequences are left in the string
521unchanged, i.e., *the backslash is left in the string*. (This behavior is
522useful when debugging: if an escape sequence is mistyped, the resulting output
523is more easily recognized as broken.) It is also important to note that the
Georg Brandl57e3b682007-08-31 08:07:45 +0000524escape sequences only recognized in string literals fall into the category of
525unrecognized escapes for bytes literals.
Georg Brandl116aa622007-08-15 14:28:22 +0000526
Georg Brandl57e3b682007-08-31 08:07:45 +0000527Even in a raw string, string quotes can be escaped with a backslash, but the
528backslash remains in the string; for example, ``r"\""`` is a valid string
529literal consisting of two characters: a backslash and a double quote; ``r"\"``
530is not a valid string literal (even a raw string cannot end in an odd number of
531backslashes). Specifically, *a raw string cannot end in a single backslash*
532(since the backslash would escape the following quote character). Note also
533that a single backslash followed by a newline is interpreted as those two
534characters as part of the string, *not* as a line continuation.
Georg Brandl116aa622007-08-15 14:28:22 +0000535
536
537.. _string-catenation:
538
539String literal concatenation
540----------------------------
541
542Multiple adjacent string literals (delimited by whitespace), possibly using
543different quoting conventions, are allowed, and their meaning is the same as
544their concatenation. Thus, ``"hello" 'world'`` is equivalent to
545``"helloworld"``. This feature can be used to reduce the number of backslashes
546needed, to split long strings conveniently across long lines, or even to add
547comments to parts of strings, for example::
548
549 re.compile("[A-Za-z_]" # letter or underscore
550 "[A-Za-z0-9_]*" # letter, digit or underscore
551 )
552
553Note that this feature is defined at the syntactical level, but implemented at
554compile time. The '+' operator must be used to concatenate string expressions
555at run time. Also note that literal concatenation can use different quoting
556styles for each component (even mixing raw strings and triple quoted strings).
557
558
559.. _numbers:
560
561Numeric literals
562----------------
563
Georg Brandlba956ae2007-11-29 17:24:34 +0000564.. index:: number, numeric literal, integer literal
565 floating point literal, hexadecimal literal
Georg Brandl57e3b682007-08-31 08:07:45 +0000566 octal literal, binary literal, decimal literal, imaginary literal, complex literal
Georg Brandl116aa622007-08-15 14:28:22 +0000567
Georg Brandl95817b32008-05-11 14:30:18 +0000568There are three types of numeric literals: integers, floating point numbers, and
569imaginary numbers. There are no complex literals (complex numbers can be formed
570by adding a real number and an imaginary number).
Georg Brandl116aa622007-08-15 14:28:22 +0000571
572Note that numeric literals do not include a sign; a phrase like ``-1`` is
573actually an expression composed of the unary operator '``-``' and the literal
574``1``.
575
576
577.. _integers:
578
579Integer literals
580----------------
581
582Integer literals are described by the following lexical definitions:
583
584.. productionlist::
Georg Brandlddee3082008-04-09 18:46:46 +0000585 integer: `decimalinteger` | `octinteger` | `hexinteger` | `bininteger`
Georg Brandl116aa622007-08-15 14:28:22 +0000586 decimalinteger: `nonzerodigit` `digit`* | "0"+
Georg Brandl57e3b682007-08-31 08:07:45 +0000587 nonzerodigit: "1"..."9"
588 digit: "0"..."9"
Georg Brandl116aa622007-08-15 14:28:22 +0000589 octinteger: "0" ("o" | "O") `octdigit`+
590 hexinteger: "0" ("x" | "X") `hexdigit`+
591 bininteger: "0" ("b" | "B") `bindigit`+
Georg Brandl116aa622007-08-15 14:28:22 +0000592 octdigit: "0"..."7"
593 hexdigit: `digit` | "a"..."f" | "A"..."F"
Georg Brandl57e3b682007-08-31 08:07:45 +0000594 bindigit: "0" | "1"
Georg Brandl116aa622007-08-15 14:28:22 +0000595
Georg Brandl57e3b682007-08-31 08:07:45 +0000596There is no limit for the length of integer literals apart from what can be
597stored in available memory.
Georg Brandl116aa622007-08-15 14:28:22 +0000598
599Note that leading zeros in a non-zero decimal number are not allowed. This is
600for disambiguation with C-style octal literals, which Python used before version
6013.0.
602
603Some examples of integer literals::
604
605 7 2147483647 0o177 0b100110111
606 3 79228162514264337593543950336 0o377 0x100000000
607 79228162514264337593543950336 0xdeadbeef
608
609
610.. _floating:
611
612Floating point literals
613-----------------------
614
615Floating point literals are described by the following lexical definitions:
616
617.. productionlist::
618 floatnumber: `pointfloat` | `exponentfloat`
619 pointfloat: [`intpart`] `fraction` | `intpart` "."
620 exponentfloat: (`intpart` | `pointfloat`) `exponent`
621 intpart: `digit`+
622 fraction: "." `digit`+
623 exponent: ("e" | "E") ["+" | "-"] `digit`+
624
625Note that the integer and exponent parts are always interpreted using radix 10.
626For example, ``077e010`` is legal, and denotes the same number as ``77e10``. The
627allowed range of floating point literals is implementation-dependent. Some
628examples of floating point literals::
629
630 3.14 10. .001 1e100 3.14e-10 0e0
631
632Note that numeric literals do not include a sign; a phrase like ``-1`` is
633actually an expression composed of the unary operator ``-`` and the literal
634``1``.
635
636
637.. _imaginary:
638
639Imaginary literals
640------------------
641
642Imaginary literals are described by the following lexical definitions:
643
644.. productionlist::
645 imagnumber: (`floatnumber` | `intpart`) ("j" | "J")
646
647An imaginary literal yields a complex number with a real part of 0.0. Complex
648numbers are represented as a pair of floating point numbers and have the same
649restrictions on their range. To create a complex number with a nonzero real
650part, add a floating point number to it, e.g., ``(3+4j)``. Some examples of
651imaginary literals::
652
653 3.14j 10.j 10j .001j 1e100j 3.14e-10j
654
655
656.. _operators:
657
658Operators
659=========
660
661.. index:: single: operators
662
663The following tokens are operators::
664
665 + - * ** / // %
666 << >> & | ^ ~
667 < > <= >= == !=
668
669
670.. _delimiters:
671
672Delimiters
673==========
674
675.. index:: single: delimiters
676
677The following tokens serve as delimiters in the grammar::
678
679 ( ) [ ] { } @
680 , : . ` = ;
681 += -= *= /= //= %=
682 &= |= ^= >>= <<= **=
683
684The period can also occur in floating-point and imaginary literals. A sequence
Georg Brandl57e3b682007-08-31 08:07:45 +0000685of three periods has a special meaning as an ellipsis literal. The second half
Georg Brandl116aa622007-08-15 14:28:22 +0000686of the list, the augmented assignment operators, serve lexically as delimiters,
687but also perform an operation.
688
689The following printing ASCII characters have special meaning as part of other
690tokens or are otherwise significant to the lexical analyzer::
691
692 ' " # \
693
Georg Brandl116aa622007-08-15 14:28:22 +0000694The following printing ASCII characters are not used in Python. Their
695occurrence outside string literals and comments is an unconditional error::
696
697 $ ?