Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

raw strings and \

2 views
Skip to first unread message

pla...@alumni.caltech.edu

unread,
Mar 5, 2006, 3:49:25 AM3/5/06
to
I thought I understood raw strings, then I got bitten by this:

x=r'c:\blah\'

which is illegal! I thought that \ had no special meanning in a raw
string so why can't it be the last character?

making me do:

x=r'c:\blah' '\\'

is just silly...

Duncan Booth

unread,
Mar 5, 2006, 4:37:03 AM3/5/06
to
wrote:

> I thought I understood raw strings, then I got bitten by this:
>
> x=r'c:\blah\'
>
> which is illegal! I thought that \ had no special meanning in a raw
> string so why can't it be the last character?

No, the backslash is still special in terms of parsing the string, it
is simply that once the string has been parsed escape sequences are not
interpreted.

>
> making me do:
>
> x=r'c:\blah' '\\'
>
> is just silly...
>
>

Yes, that is silly. You could alternatively do:

x = 'c:/blah/'

or

x = 'c:\\blah\\'

But why would you want to end your path with a literal backslash anyway?
Get into the habit of using the os.path module for all manipulations of
file paths and you never need to worry about this sort of thing again.

This sort of thing should work well enough:

folder = 'c:/blah'

for line in open(os.path.join(folder, 'data.txt')):
print line

pla...@alumni.caltech.edu

unread,
Mar 5, 2006, 11:27:31 AM3/5/06
to
Hi Duncan,

thanks for the reply. I figured that this was a technical problem
associated with the parser.

This one is going on my Python gotchas list. It is really silly from
an end user perspective to have \ not special in raw strings _except_
if it is the last character.

Alex Martelli

unread,
Mar 5, 2006, 11:57:32 AM3/5/06
to
<pla...@alumni.caltech.edu> wrote:

The alternative would have been to offer no way at all for a rawstring
to include its quoting-character. Since rawstrings were designed to
support regular expressions (which never need to end with a backslash),
that was considered a higher cost -- there is no real advantage to
supporting backslash-using Dos/Windows path literals, even though many
Windowsers use rawstrings for those.


Alex

Blackbird

unread,
Mar 5, 2006, 11:58:44 AM3/5/06
to

But at the parsing stage, it *is* somewhat special, even if it is not the
last charater. E.g,

a = r'check \' this'
print a

The second quote is not regarded as ending the string literal.


Steven D'Aprano

unread,
Mar 5, 2006, 12:05:04 PM3/5/06
to

I don't deny that this is a gotcha, but you misunderstand the purpose of
raw strings. They weren't designed so that Windows users could enter
pathnames with backslashes. Raw strings are designed to enter regular
expressions, and for regular expressions, not being able to end a string
with a backslash is not a bug but a feature.

See http://www.ferg.org/projects/python_gotchas.html#contents_item_2

(and try not to choke on the oh-so-saccharine-sweet cutesy introduction.
Red Ridinghood indeed *wink*)


--
Steven.

pla...@alumni.caltech.edu

unread,
Mar 5, 2006, 12:43:43 PM3/5/06
to
Hi Alex,

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?

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).

Again, thanks for taking the time to reply.

Now get back to work on your new Nutshell book :-)

pla...@alumni.caltech.edu

unread,
Mar 5, 2006, 12:47:06 PM3/5/06
to
Hi,

thanks for the reply. I was not aware of this in raw strings (and
frankly, don't like it... but who cares about that :-) )

When I needed embedded quotes in a raw string I went the triple quote
route:

a = r'''check \' this'''

which makes more sense to me.

pla...@alumni.caltech.edu

unread,
Mar 5, 2006, 12:53:53 PM3/5/06
to
Hi Steven,

thanks for the reply. I was/am aware that raw strings are mainly used
for regular expressions (and franky that was I usually use them for).
I was not aware that they still have "special powers" in raw strings
(thanks for the link!).

This one bit me when I was doing some quick and dirty hacking and a 5
minute task ended up being a much longer adventure becuase of this
(Note to self: don't post when annoyed).

Alex Martelli

unread,
Mar 5, 2006, 2:02:46 PM3/5/06
to
<pla...@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

Steve Holden

unread,
Mar 5, 2006, 9:24:34 PM3/5/06
to pytho...@python.org
pla...@alumni.caltech.edu wrote:
> Hi,
>
> thanks for the reply. I was not aware of this in raw strings (and
> frankly, don't like it... but who cares about that :-) )
>
Healthy attitude!

> When I needed embedded quotes in a raw string I went the triple quote
> route:
>
> a = r'''check \' this'''
>
> which makes more sense to me.
>

But remember you could equally well have used

a = "check \\' this"

I presume you do *want* that backslash in there? If not, of course, then

a = "check ' this"

is by far the simplest way to represent what you want. because Python
offers four different string quotes, each of which can be raw, there's
always a way ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

pla...@alumni.caltech.edu

unread,
Mar 5, 2006, 10:47:07 PM3/5/06
to
>Alas, somebody will now quote Emerson at you, I fear;-).

Let them come :-)

I almost always see this (mis)quoted as:

"consistency is the hobgoblin of little minds"

which is not really what Emerson said. The full quote is:

"A foolish consistency is the hobgoblin of little minds"

I would say that a foolish anything is something to avoid. I don't
think that anyone would claim that inconsistency is a virtue in a
computer language (or in anything else for that matter).

Blackbird

unread,
Mar 6, 2006, 4:43:14 AM3/6/06
to
Slightly OT, but here is a crazy little program that shows the power of
using raw strings:

s=r'print "s=r\'%s\'\n%s"%(s,s)'
print "s=r\'%s\'\n%s"%(s,s)

When run, this program will print an exact copy of itself.

Blackbird


Steven D'Aprano

unread,
Mar 6, 2006, 7:14:57 AM3/6/06
to
Almost off-topic.

Well, okay, completely off-topic.

On Sun, 05 Mar 2006 19:47:07 -0800, plahey wrote:

> I would say that a foolish anything is something to avoid. I don't
> think that anyone would claim that inconsistency is a virtue in a
> computer language (or in anything else for that matter).

Inconsistency can be a virtue whenever predictability is a vice.

Imagine, for example, that Fred and Bill are negotiating over something
(say, Fred wants to buy Bill's house). If Fred is predictable, then Bill
can work out just how high Fred is willing to pay, and refuse to accept a
penny less. In other words, if you are too consistent, people will learn
to take advantage of that consistency to get the most from you for the
least in return.

On the other hand, if Fred is unpredictable -- that is, inconsistent -- it
is much harder to predict his behaviour, and Bill will be more cautious
and more likely to settle for a lower offer.

Think about hostile negotiations. If people know exactly how far they
can push you before you will get mad, they will push right to the edge.
But if they don't know where that edge lies, if they are uncertain how you
will react ("if I demand the Sudetenland, will he break off negotiations
and launch a preemptive attack?"), they will be more cautious, less
demanding, more open to compromise.

Cognitive scientists believe that the benefits of unpredictability and
inconsistency are behind the evolution of such irrational emotions as
jealousy and rage. In that sense, their very irrationality is what makes
them rational.


--
Steven.

Fredrik Lundh

unread,
Mar 6, 2006, 7:19:39 AM3/6/06
to pytho...@python.org
"Blackbird" <fa...@nospam.no> wrote:

I'm not sure what the raw strings brings to the table, though; it's not like you need
them to write a "self-replicating" python program:

http://miscoranda.com/37

</F>

Blackbird

unread,
Mar 6, 2006, 7:59:31 AM3/6/06
to


Great link! I wasn't aware that this was called a Quine. This one is
great:

_='_=%r;print _%%_';print _%_

When I hacked down the two lines, I didnt see that assigning the variable to
the beginning of the program, instead of the end, essentially gets rid of
literals in the actual print statement. And now I also learned about %r.


Fredrik Lundh

unread,
Mar 6, 2006, 10:11:44 AM3/6/06
to pytho...@python.org
pla...@alumni.caltech.edu wrote:

> 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

you seem to be missing that "exception" you think you're seeing is
the consequence of a consistency: *all* literal strings are parsed
in the same way, no matter what's prefix qualifier they're using.

</F>

John Salerno

unread,
Mar 6, 2006, 4:49:51 PM3/6/06
to
Alex Martelli wrote:

>> Now get back to work on your new Nutshell book :-)
>
> Yep, good point!-)

Are you working on a new edition? I didn't see any new Python books
listed on O'Reilly's site through April, but I'd definitely be
interested in new versions of the books I plan to get soon (Cookbook and
Nutshell, for example, although I figure the Cookbook is fairly new at
this point.)

pla...@alumni.caltech.edu

unread,
Mar 6, 2006, 7:41:04 PM3/6/06
to
Interesting, so thats where this comes from.

Of course the problem is that I thought that a raw string was a
different animal than a regular string (i.e. truely "raw" where no
characters are special, except of course the closing quote) so parsing
consistency is surprising to an end user (who has an overly-simplistic
mental model).

I'm sure that this choice greatly simplifies the internals of Python.
As an end user, of course, I couldn't care less about this (just as my
end users don't care about my problems :-) ). I would still prefer my
still-bloody raw strings, but I can live with the slightly-cooked ones
now that I've got it...

Thanks!

Alex Martelli

unread,
Mar 7, 2006, 12:02:22 AM3/7/06
to
John Salerno <john...@NOSPAMgmail.com> wrote:

Yep, a 3rd edition of the Cookbook is not in the cards. A 2nd edition
of the Nutshell is, and if all goes well should be out before the fall.


Alex

John Salerno

unread,
Mar 7, 2006, 10:14:06 AM3/7/06
to

Awesome, can't wait! :)

0 new messages