Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions Doc/faq/design.rst
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,21 @@ backslash at the end escapes the closing quote character, leaving an
unterminated string.

Raw strings were designed to ease creating input for processors (chiefly regular
expression engines) that want to do their own backslash escape processing. Such
processors consider an unmatched trailing backslash to be an error anyway, so
raw strings disallow that. In return, they allow you to pass on the string
quote character by escaping it with a backslash. These rules work well when
r-strings are used for their intended purpose.
expression engines) that want to do their own backslash escape processing, so
backslashes are generally not processed specially at the syntax level. However,
there needs to be a way to include the string's quote character itself inside a
raw string. Because these processors do their own backslash handling, it is
safe to send them an extra backslash before a quote character. Python therefore
interprets a backslash followed by the quote that opened the string as two
literal characters in the string's value: the backslash is retained. For
example, ``r"\""`` contains a backslash and a double quote, not just a double
quote.

Because of this interpretation, a backslash followed by a closing quote already
has a meaning: the quote does not end the string. Consequently no syntax can
express a single trailing backslash in a raw string, and a quote character can
only appear when preceded by a literal backslash in the string's value. The
same rules apply to triple-quoted raw strings.

If you're trying to build Windows pathnames, note that all Windows system calls
accept forward slashes too::
Expand Down
23 changes: 13 additions & 10 deletions Doc/reference/lexical_analysis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1003,8 +1003,10 @@ Raw string literals

Both string and bytes literals may optionally be prefixed with a letter '``r``'
or '``R``'; such constructs are called :dfn:`raw string literals`
and :dfn:`raw bytes literals` respectively and treat backslashes as
literal characters.
and :dfn:`raw bytes literals` respectively. In these literals, backslashes are
not processed as escape sequences and appear unchanged in the string's value,
except that a backslash before the quote character that opened the literal
prevents that quote from ending the literal; the backslash itself is retained.
As a result, in raw string literals, :ref:`escape sequences <escape-sequences>`
are not treated specially:

Expand All @@ -1013,14 +1015,15 @@ are not treated specially:
>>> r'\d{4}-\d{2}-\d{2}'
'\\d{4}-\\d{2}-\\d{2}'

Even in a raw literal, quotes can be escaped with a backslash, but the
backslash remains in the result; for example, ``r"\""`` is a valid string
literal consisting of two characters: a backslash and a double quote; ``r"\"``
is not a valid string literal (even a raw string cannot end in an odd number of
backslashes). Specifically, *a raw literal cannot end in a single backslash*
(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, *not* as a line continuation.
Even in a raw literal, a backslash before the quote character that opened the
literal prevents that quote from ending the literal, but the backslash remains
in the result; for example, ``r"\""`` is a valid string literal consisting of
two characters: a backslash and a double quote; ``r"\"`` is not a valid string
literal (even a raw string cannot end in an odd number of backslashes).
Specifically, *a raw literal cannot end in a single backslash* (since the
backslash would prevent the following quote character from ending the literal).
Note also that a single backslash followed by a newline is interpreted as those
two characters as part of the literal, *not* as a line continuation.


.. index::
Expand Down
Loading