By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,425 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

raw strings and \

P: n/a
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...

Mar 5 '06 #1
Share this Question
Share on Google+
20 Replies


P: n/a
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
Mar 5 '06 #2

P: n/a
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.

Mar 5 '06 #3

P: n/a
<pl****@alumni.caltech.edu> wrote:
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.


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
Mar 5 '06 #4

P: n/a
pl****@alumni.caltech.edu wrote:
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.


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.


Mar 5 '06 #5

P: n/a
On Sun, 05 Mar 2006 08:27:31 -0800, plahey wrote:
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.


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_...ontents_item_2

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

Mar 5 '06 #6

P: n/a
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 :-)

Mar 5 '06 #7

P: n/a
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.

Mar 5 '06 #8

P: n/a
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).

Mar 5 '06 #9

P: n/a
<pl****@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
Mar 5 '06 #10

P: n/a
pl****@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

Mar 6 '06 #11

P: n/a
>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).

Mar 6 '06 #12

P: n/a
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
Mar 6 '06 #13

P: n/a
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.

Mar 6 '06 #14

P: n/a
"Blackbird" <fa**@nospam.no> wrote:
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.


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>

Mar 6 '06 #15

P: n/a
Fredrik Lundh wrote:
"Blackbird" <fa**@nospam.no> wrote:
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.


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

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.
Mar 6 '06 #16

P: n/a
pl****@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>

Mar 6 '06 #17

P: n/a
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.)
Mar 6 '06 #18

P: n/a
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!

Mar 7 '06 #19

P: n/a
John Salerno <jo******@NOSPAMgmail.com> wrote:
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.)


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
Mar 7 '06 #20

P: n/a
Alex Martelli wrote:
John Salerno <jo******@NOSPAMgmail.com> wrote:
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.)


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


Awesome, can't wait! :)
Mar 7 '06 #21

This discussion thread is closed

Replies have been disabled for this discussion.