String handling bug in Python

Steve Holden sholden at holdenweb.com
Tue Apr 23 17:59:42 EDT 2002


"Colin Brown" <cbrown at metservice.com> wrote in message
news:3cc5d5c8$1 at news.nz.asiaonline.net...
> I have entered the following at the command line in Python 2.1.1 on Linux
> and in Python 2.2 on NT and get the same results:
>
> >>> 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 correct, and the documentation explains why in Section 2.4.1 of the
Python Reference Manual:

"""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). Specifically, a raw string
cannot end in a single backslash (since the backslash would escape the
following quote character).""" (italics omitted to avoid posting HTML)

regards
 Steve
--

home: http://www.holdenweb.com/
Python Web Programming:
http://pydish.holdenweb.com/pwp






More information about the Python-list mailing list