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

Raw strings and escaping

P: n/a
Hi,

I would expect this to work,

rawstring=r'some things\new things\some other things\'

But it fails as the last backslash escapes the single quote.

...although writing this I think I have solved my own problem. Is \' the
only thing escaped in a raw string so you can place ' in a raw string?
Although I thought the correct thing in that case would be;

rawstring=r"rawstring'with single-quote"
This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email fromyour computer.

You should not copy the email, use it for any purpose or disclose its contents to any other person.
Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions ofDigica.
It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com
Oct 3 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Matthew Warren wrote in news:mailman.1152.1159872720.10491.python-
li**@python.org in comp.lang.python:
I would expect this to work,

rawstring=r'some things\new things\some other things\'
It in the docs:

<url:http://docs.python.org/ref/strings.html#l2h-14>

.... Specifically, a raw string cannot end in a single backslash (since the
backslash would escape the following quote character). Note also that a
single backslash followed by a newline is interpreted as those two
characters as part of the string, not as a line continuation.

Rob.
Oct 3 '06 #2

P: n/a
"Matthew Warren" <Ma************@Digica.comwrote:
I would expect this to work,

rawstring=r'some things\new things\some other things\'

But it fails as the last backslash escapes the single quote.

..although writing this I think I have solved my own problem. Is \'
the only thing escaped in a raw string so you can place ' in a raw
string? Although I thought the correct thing in that case would be;

rawstring=r"rawstring'with single-quote"
You cannot end *any* string literal with an odd number of backslash
characters. The "r" prefix changes how the escape sequences are interpreted
after the string literal has been parsed, it doesn't change how the literal
itself is actually parsed. See the Python Reference Manual, 2.4.1:
When an "r" or "R" prefix is present, a character following a
backslash is included in the string without change, and all
backslashes are left in the string. For example, the string literal
r"\n" consists of two characters: a backslash and a lowercase "n".
String quotes can be escaped with a backslash, but the backslash
remains in the string; for example, r"\"" is a valid string literal
consisting of two characters: a backslash and a double quote; r"\" is
not a valid string literal (even a raw string cannot end in an odd
number of backslashes). Specifically, a raw string cannot end in a
single backslash (since the backslash would escape the following quote
character). Note also that a single backslash followed by a newline is
interpreted as those two characters as part of the string, not as a
line continuation.
Oct 3 '06 #3

P: n/a
In article <ma***************************************@python. org>, Matthew Warren wrote:
I would expect this to work,

rawstring=r'some things\new things\some other things\'

But it fails as the last backslash escapes the single quote.
String constants in Python are weird - raw strings doubly so.
In a raw string, backslashes are not special - unless followed by
a quote character, when they simultaneously escape the quote and
also insert a literal backslash.

I presume there was originally some reason for this bizarre behaviour
- it'd be interesting to know what it is/was, if anyone knows?
Oct 3 '06 #4

P: n/a
Jon Ribbens <jo********@unequivocal.co.ukwrote:
I presume there was originally some reason for this bizarre behaviour
- it'd be interesting to know what it is/was, if anyone knows?
See the FAQ for the explanation:

http://www.python.org/doc/faq/genera...th-a-backslash

The idea is that if you use raw strings for regular expressions you can
write things like:

pattern = r'[\'"]'

if the raw string simply ignored the backslash altogether it would be much
harder to write regular expressions that contain both sorts of quotes.
Oct 3 '06 #5

P: n/a
In article <Xn*************************@127.0.0.1>, Duncan Booth wrote:
>I presume there was originally some reason for this bizarre behaviour
- it'd be interesting to know what it is/was, if anyone knows?
See the FAQ for the explanation:

http://www.python.org/doc/faq/genera...th-a-backslash

The idea is that if you use raw strings for regular expressions you can
write things like:

pattern = r'[\'"]'

if the raw string simply ignored the backslash altogether it would be much
harder to write regular expressions that contain both sorts of quotes.
Well, hardly *much* harder:

pattern = r"""foo"""

Personally, I think that raw strings behaving as they do is
unexpected, unintuitive and unnecessary, but it's obviously too late
to change it now anyway ;-)

I think standard strings accepting backslash followed by an unexpected
character is also a mistake, but again it's too late to fix now.
Oct 3 '06 #6

P: n/a
Jon Ribbens <jo********@unequivocal.co.ukwrote:
Well, hardly *much* harder:

pattern = r"""foo"""
It means you have to always triple quote your raw strings or know in
advance of writing the regular expression which of r'', r"", r'''''',
r"""""" is most appropriate. The way it works at the moment you don't have
to care, you just write the raw string knowing that with r'' you have to
escape single quotes, r"" you have to escape double quotes and everything
works as expected.

Its only when you start trying to use raw strings for things other than
regular expressions that backslash at the end of the string can be a
problem.
Oct 3 '06 #7

P: n/a
Matthew Warren wrote:
Hi,

I would expect this to work,

rawstring=r'some things\new things\some other things\'

But it fails as the last backslash escapes the single quote.
Note something many people don't when looking over the string rules:

astring = r'some things\new things\some other things' '\\'

gives you exactly what you want, doesn't imply a string concatenation
at run time, and various other things. Two strings in succession are
concatenated at source translation time.

By the way, I renamed the result from being rawstring. It gives people
bad intuitions to refer to some strings as "raw" when what you really
mean is that the notation you are using is a "raw" notation for a
perfectly normal string.

--Scott David Daniels
sc***********@acm.org
Oct 3 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.