r'\' - python parser bug?
Fuzzyman
michael at foord.net
Wed May 26 10:32:44 EDT 2004
Duncan Booth <me at privacy.net> wrote in message news:<Xns94F55D228C7AAduncanrcpcouk at 127.0.0.1>...
> michael at foord.net (Fuzzyman) wrote in
> news:8089854e.0405252339.18e0c59d at posting.google.com:
>
> >>>> print r'c:\subdir\'
> SyntaxError: EOL while scanning single-quoted string
> >>>>
>
> >> When the parser sees a backslash inside
> >> a string literal, it always skips the next character.
> > In the above example the parser *only* skips the next character if it
> > is at the end of the string... surely illogical. The reason given is
> > effectively 'raw strings were created for regular expressions, so it
> > doesn't matter if the behaviour is illogical' (and precludes other
> > reasonable uses!!)..........
> >
>
> In a python string, backslash is an escape character which gives the next
> character(s) special meaning, so '\n' is a single newline character. If the
> escaped character isn't a known escape then the parser simply passes
> through the entire sequence. So '\s' is a two character string. In all
> cases at least one character following the backslash is parsed when the
> backslash is encountered, and this character can never form part of the
> string terminator.
>
> Raw strings are processed in exactly the same way as normal strings, except
> that no escape sequences are recognised, however the character following
> the backslash is still prevented from terminating the string, just as it
> would in any other string. This *useful*? behaviour allows you to put
> single and double quotes into a raw string provided that they are preceded
> by a backslash.
>
> print r'c:\subdir\'file'
>
> Raw strings aren't intended for writing DOS pathnames, they are actually
> targetted for regular expressions where this behaviour makes more sense.
>
[snip..]
Yeah.. that's not an annoying feature.... I mean no-one would ever
want to use strings to hold Windows pathnames in......
Regards,
Fuzzy
More information about the Python-list
mailing list