Raw string question

Robin Thomas robin.thomas at starmedia.net
Tue Feb 27 11:50:50 EST 2001


At 09:12 AM 2/27/01 -0600, Larry Bates wrote:
>Why can't a raw string end with a backslash (\)?
>
>example
>
>a=r"c:\path\"

Below from Python Language Reference 2.4.1. The text explains the behavior 
and also gives you good hints about how raw string literals are parsed in 
the current implementation.

"""
Unlike Standard C, all unrecognized escape sequences are left in the string 
unchanged, i.e., the backslash is left in the string. (This behavior is 
useful when debugging: if an escape sequence is mistyped, the resulting 
output is more easily recognized as broken.)

When an `r' or `R' prefix is present, backslashes are still used to quote 
the following character, but all backslashes are left in the string. For 
example, the string literal r"\n" consists of two characters: a backslash 
and a lowercase `n'. String quotes can be escaped with a backslash, but the 
backslash remains in the string; for example, r"\"" is a valid string 
literal consisting of two characters: a backslash and a double quote; r"\" 
is not a value string literal (even a raw string cannot end in an odd 
number of backslashes). Specifically, a raw string 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 string, not as a line continuation.
"""


--
Robin Thomas
Engineering
StarMedia Network, Inc.
robin.thomas at starmedia.net





More information about the Python-list mailing list