what is wrong with that r"\"
nick at craig-wood.com
Wed Jul 4 11:30:04 CEST 2007
Neil Cerutti <horpner at yahoo.com> wrote:
> From the Python Language Reference 2.4.1 String Literals:
> When an "r" or "R" prefix is present, a character following a
> backslash is included in the string without change, and all
> backslashes are left in the string. For example, the string
> literal r"\n" consists of two characters: a backslash and a
> lowercase "n".
So far so good.
> 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;
a) That is weird! Why would you ever want to do that - ie insert \"
into your string as a special case? If I wanted a " in a raw string
then I'd use a r'' string or a triple quoted string.
> r"\" is not a valid 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).
b) That is a logical consequence of a)
> 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.
As I'd expect.
If we removed a) then we could remove b) also and r"" strings would
work as everyone expects.
Does anyone know the justification for a)? Maybe we should remove it
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list