Raw String Question
Nick Craig-Wood
nick at craig-wood.com
Thu Mar 12 15:30:04 EDT 2009
Jim Garrison <jgarrison at troux.com> wrote:
> >>> r"a\b"
> 'a\\b'
> >>> r"a\"
> SyntaxError: EOL while scanning string literal (<pyshell#45>, line 1)
> >>> r"a\ "
> 'a\\ '
> >>> r"a\""
> 'a\\"'
>
> It seems the parser is interpreting the backslash as an escape
> character in a raw string if the backslash is the last character.
> Is this expected?
Yes
http://docs.python.org/reference/lexical_analysis.html#string-literals
Specifically, a raw string cannot end in a single backslash (since
the backslash would escape the following quote character).
The usual way round this is like this
>>> r"a" "\\"
'a\\'
>>>
Which isn't terribly elegant, but it doesn't happen very often.
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list