2. Lexical analysis
Keywords: ref

#
2. Lexical analysis

A Python program is read by a parser. Input to the parser is a stream of tokens, generated by the lexical analyzer. This chapter describes how the lexical analyzer breaks a file into tokens.

Python uses the 7-bit ASCII character set for program text and string literals. 8-bit characters may be used in string literals and comments but their interpretation is platform dependent; the proper way to insert 8-bit characters in string literals is by using octal or hexadecimal escape sequences.

The run-time character set depends on the I/O devices connected to the program but is generally a superset of ASCII.

Future compatibility note: It may be tempting to assume that the character set for 8-bit characters is ISO Latin-1 (an ASCII superset that covers most western languages that use the Latin alphabet), but it is possible that in the future Unicode text editors will become common. These generally use the UTF-8 encoding, which is also an ASCII superset, but with very different use for the characters with ordinals 128-255. While there is no consensus on this subject yet, it is unwise to assume either Latin-1 or UTF-8, even though the current implementation appears to favor Latin-1. This applies both to the source character set and the run-time character set.

Subsections

- 2.1 Line structure
  - 2.1.1 Logical lines
  - 2.1.2 Physical lines
  - 2.1.3 Comments
  - 2.1.4 Explicit line joining
  - 2.1.5 Implicit line joining
  - 2.1.6 Blank lines
  - 2.1.7 Indentation
  - 2.1.8 Whitespace between tokens
- 2.2 Other tokens
- 2.3 Identifiers and keywords
  - 2.3.1 Keywords
  - 2.3.2 Reserved classes of identifiers
- 2.4 Literals
  - 2.4.1 String literals
  - 2.4.2 String literal concatenation
  - 2.4.3 Numeric literals
  - 2.4.4 Integer and long integer literals
  - 2.4.5 Floating point literals
  - 2.4.6 Imaginary literals
- 2.5 Operators
- 2.6 Delimiters