Text of docs.python.org › id › 3 › reference › lexical_analysis.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

2. Lexical analysis — Dokumentasi Python 3.15.2

Navigasi index modul | berikut | sebelum | Python » 3.15.2 documentation » Referensi Bahasa Python » 2. Lexical analysis | Tema | 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 (also known as the tokenizer ). This chapter describes how the lexical analyzer produces these tokens. The lexical analyzer determines the program text's encoding (UTF-8 by default), and decodes the text into source characters .

If the text cannot be decoded, a SyntaxError is raised. Next, the lexical analyzer uses the source characters to generate a stream of tokens. The type of a generated token generally depends on the next source character to be processed. Similarly, other special behavior of the analyzer depends on the first source character that hasn't yet been processed. The following table gives a quick summary of these source characters, with links to sections that contain more information.

Karakter Next token (or other relevant documentation) space tab formfeed Whitespace CR, LF New line Indentation backslash ( \ ) Explicit line joining (Also significant in string escape sequences ) hash ( # ) Comment quote ( ' , " ) String literal ASCII letter ( a - z , A - Z ) non-ASCII character Name Prefixed string or bytes literal underscore ( _ ) Name (Can also be part of numeric literals ) number ( 0 - 9 ) Numeric literal dot ( . ) Numeric literal Operator question mark ( ? ) dollar ( $ ) backquote ( ​`​ ) control character Error (outside string literals and comments) other printing character Operator or delimiter end of file End marker 2.1.

Line structure ¶ A Python program is divided into a number of logical lines . 2.1.1. Logical lines ¶ 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 physical lines by following the explicit or implicit line joining rules.

2.1.2. Physical lines ¶ A physical line is a sequence of characters terminated by one the following end-of-line sequences: the Unix form using ASCII LF (linefeed), the Windows form using the ASCII sequence CR LF (return followed by linefeed), the ' Classic Mac OS ' form using the ASCII CR (return) character. Regardless of platform, each of these sequences is replaced by a single ASCII LF (linefeed) character. (This is done even inside string literals .) Each line can use any of the sequences; they do not need to be consistent within a file.

The end of input also serves as an implicit terminator for the final physical line. Formally: newline : <ASCII LF> | <ASCII CR> <ASCII LF> | <ASCII CR> 2.1.3. Comments ¶ A comment starts with a hash character ( # ) 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.

2.1.4. Encoding declarations ¶ If a comment in the first or second line of the Python script matches the regular expression coding[=:]\s*([-\w.]+) , 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 # -*- coding: <encoding-name> -*- which is recognized also by GNU Emacs, and # vim:fileencoding=<encoding-name> which is recognized by Bram Moolenaar's VIM.

If no encoding declaration is found, the default encoding is UTF-8. If the implicit or explicit encoding of a file is UTF-8, an initial UTF-8 byte-order mark ( b'\xef\xbb\xbf' ) is ignored rather than being a syntax error. If an encoding is declared, the encoding name must be recognized by Python (see Standard Encodings ). The encoding is used for all lexical analysis, including string literals, comments and identifiers.

All lexical analysis, including string literals, comments and identifiers, works on Unicode text decoded using the source encoding. Any Unicode code point, except the NUL control character, can appear in Python source. source_character : <any Unicode code point, except NUL> 2.1.5. Explicit line joining ¶ Two or more physical lines may be joined into logical lines using backslash characters ( \ ), 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: if 1900 < year < 2100 and 1 <= month <= 12 \ and 1 <= day <= 31 and 0 <= hour < 24 \ and 0 <= minute < 60 and 0 <= second < 60 : # Looks like a valid date return 1 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. 2.1.6. Implicit line joining ¶ Expressions in parentheses, square brackets or curly braces can be split over more than one physical line without using backslashes. For example: 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 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. 2.1.7. Blank lines ¶ 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 (that is, one containing not even whitespace or a comment) terminates a multi-line statement. 2.1.8. Indentation ¶ 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.

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.

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 TabError is raised in that case. Cross-platform compatibility note: 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.

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). The indentation levels of consecutive lines are used to generate INDENT and DEDENT tokens, using a stack, as follows. Before the first line of the file

The rest of this page is on the live site.