String handling bug in Python

Andrew Dalke dalke at dalkescientific.com
Tue Apr 23 17:55:31 EDT 2002


Colin Brown:
>>>> r'\'
>  File "<string>", line 1
>     r'\'
>        ^
>SyntaxError: invalid token
>>>>
>
>It appears as if the r option for strings does not handle a backslash as
the
>last string character properly.

That's the documented behaviour for raw strings, down to the exact
example you gave.  Why do you expect it to be otherwise?

http://python.org/doc/current/ref/strings.html
] 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'. 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 valid string literal (even
] a raw string cannot end in an odd number of backslashes).

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list