what is wrong with that r"\"

A

alf

question without words:
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string'\\ '
 
K

kyosohma

I wonder if the OP was asking how to spell the one-length string \?
In that case, the answer is that it can't be done using raw strings,
but "\\" does it. Backslash escapes aren't interpreted in raw strings,
but you still can't end a raw string with a backslash.

Jean-Paul

Very true...sometimes I need to read these weird posts 2 or 3 times.

Mike
 
N

Neil Cerutti

question without words:

File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
'\\ '

From the Python Language Reference 2.4.1 String Literals:

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

Nick Craig-Wood

Neil Cerutti said:
From the Python Language Reference 2.4.1 String Literals:

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

So far so good.
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;

a) That is weird! Why would you ever want to do that - ie insert \"
into your string as a special case? If I wanted a " in a raw string
then I'd use a r'' string or a triple quoted string.
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).

b) That is a logical consequence of a)
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.

As I'd expect.

If we removed a) then we could remove b) also and r"" strings would
work as everyone expects.

Does anyone know the justification for a)? Maybe we should remove it
in py3k?
 
N

Nick Craig-Wood

One slash escapes the following character, so the proper way of
writing it is either

r"\\" or r"\""

I don't think so...
'\\"'

Indicating that all the \ in the above are inserted literally.

Maybe you meant
'"'
 
N

Neil Cerutti

a) That is weird! Why would you ever want to do that - ie
insert \" into your string as a special case? If I wanted a "
in a raw string then I'd use a r'' string or a triple quoted
string.

If we removed a) then we could remove b) also and r"" strings
would work as everyone expects.

Does anyone know the justification for a)? Maybe we should
remove it in py3k?

If the escaped quotes didn't function in raw strings, I'd be
unable to construct (with a single notation) a regex that
included both kinds of quotes at once.

re.compile(r"'\"")
 
M

Marc 'BlackJack' Rintsch

If the escaped quotes didn't function in raw strings, I'd be
unable to construct (with a single notation) a regex that
included both kinds of quotes at once.

re.compile(r"'\"")

Where's the problem!? ::

re.compile(r''''"''')

Ah, I see -- readability is the problem. :)

Ciao,
Marc 'BlackJack' Rintsch
 
D

Duncan Booth

Nick Craig-Wood said:
Does anyone know the justification for a)? Maybe we should remove it
in py3k?

I think at least part of the justification is that it keeps the grammar
simple. The tokenising happens identically irrespective of any modifiers.
The r modifier doesn't actually change the syntax of the string at all,
just the interpretation of the token.

If you changed this then the grammar would become slightly more complex.
 
N

Neil Cerutti

That incurs the problem that you can't end with a '. So you
can't end with *something* either way.

I take that back. With good planning, you can end with whichever
you need by choosing the other delimiter.

Oh well. It seemed like a good explanation at first.
 
N

Nick Craig-Wood

Marc 'BlackJack' Rintsch said:
Where's the problem!? ::

re.compile(r''''"''')

Ah, I see -- readability is the problem. :)

Actually the problem is that those backslashes don't actually
disappear thus producing non-intuititive behaviour. The example you
provided produces a different result to Neil's result.

Neil is correct in saying that his example works for regexp matching
though, as the regexp matcher understands \" as being the same as ".

So r"" strings work well as Regexp-strings but not so well as
Raw-strings IMHO.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top