diff --git a/Doc/faq/design.rst b/Doc/faq/design.rst index ca5fcc154f73127..14c945f413f59f2 100644 --- a/Doc/faq/design.rst +++ b/Doc/faq/design.rst @@ -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:: diff --git a/Doc/reference/lexical_analysis.rst b/Doc/reference/lexical_analysis.rst index 6f7f352db3c4383..cd4207a3f55bd4b 100644 --- a/Doc/reference/lexical_analysis.rst +++ b/Doc/reference/lexical_analysis.rst @@ -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 ` are not treated specially: @@ -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::