raw strings and \

Alex Martelli aleaxit at yahoo.com
Sun Mar 5 20:02:46 CET 2006


<plahey at alumni.caltech.edu> wrote:

> thanks for the reply.  I can see that there is a choice that needed to
> be made.  Before I was aware of the \ issue I just used (yes it has
> come up, but the example is at work) triple quotes to work around the
> embedded quote issue:
> 
> x=r'''It's like this "c:\blah\" ok?'''
> print x
> It's like this "c:\blah\" ok?
> 
> Am I missing something?

No, triplequotes are perfectly valid.

> I guess my point is that it is quite surprising that r'\' is not a
> legal way to represent a backslash.  I look for consistency in the
> tools that I work with and this except-for-the-last-character exception
> is annoying (I admit it might be a silly use-case but consistency is
> consistency).

Alas, somebody will now quote Emerson at you, I fear;-).  Me, I
appreciate consistency and regularity more than most (without making a
fetish of it), but the rule "a rawstring cannot end with an odd number
of backslashes" is perfectly regular and consistent, so I'm not
perturbed by it (it's NOT about the last character, as r'\\' is just
fine even though its last character is a \ -- it's about the parity of
the length of the terminating sequence of backslashes: even is OK [0 is
of course even;-)] and odd is not).

> Again, thanks for taking the time to reply.
> 
> Now get back to work on your new Nutshell book :-)

Yep, good point!-)


Alex



More information about the Python-list mailing list