473,396 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

what is wrong with that r"\"

alf
question without words:
>>r"\"
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
>>r"\ "
'\\ '
Jul 3 '07 #1
10 1828
On Jul 3, 7:15 am, alf <ask@mewrote:
question without words:
>>r"\"
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
>>r"\ "
'\\ '
One slash escapes the following character, so the proper way of
writing it is either

r"\\" or r"\""

See http://docs.python.org/ref/strings.html for more information.

Mike

Jul 3 '07 #2
On Jul 3, 8:25 am, Jean-Paul Calderone <exar...@divmod.comwrote:
On Tue, 03 Jul 2007 06:16:43 -0700, kyoso...@gmail.com wrote:
On Jul 3, 7:15 am, alf <ask@mewrote:
question without words:
>>r"\"
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
>>r"\ "
'\\ '
One slash escapes the following character, so the proper way of
writing it is either
r"\\" or r"\""
Seehttp://docs.python.org/ref/strings.htmlfor more information.

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

Jul 3 '07 #3
On 2007-07-03, alf <ask@mewrote:
question without words:
>r"\"
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
>r"\ "
'\\ '
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.

--
Neil Cerutti
Ask about our plans for owning your home --sign at mortgage company
Jul 3 '07 #4
Neil Cerutti <ho*****@yahoo.comwrote:
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?

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 4 '07 #5
ky******@gmail.com <ky******@gmail.comwrote:
On Jul 3, 7:15 am, alf <ask@mewrote:
question without words:
>>r"\"
File "<stdin>", line 1
r"\"
^
SyntaxError: EOL while scanning single-quoted string
>>r"\ "
'\\ '

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

r"\\" or r"\""
I don't think so...
>>r"\\"
'\\\\'
>>r"\""
'\\"'

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

Maybe you meant
>>"\\"
'\\'
>>"\""
'"'

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jul 4 '07 #6
On 2007-07-04, Nick Craig-Wood <ni**@craig-wood.comwrote:
> 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.

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"'\"")

--
Neil Cerutti
Jul 4 '07 #7
On Wed, 04 Jul 2007 11:21:14 +0000, Neil Cerutti wrote:
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
Jul 4 '07 #8
Nick Craig-Wood <ni**@craig-wood.comwrote:
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.
Jul 4 '07 #9
On 2007-07-04, Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
On Wed, 04 Jul 2007 11:21:14 +0000, Neil Cerutti wrote:
>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''''"''')
That incurs the problem that you can't end with a '. So you can't
end with *something* either way.

--
Neil Cerutti
Jul 4 '07 #10
On 2007-07-04, Neil Cerutti <ho*****@yahoo.comwrote:
On 2007-07-04, Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
>On Wed, 04 Jul 2007 11:21:14 +0000, Neil Cerutti wrote:
>>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''''"''')

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.

--
Neil Cerutti
Jul 4 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ken Varn | last post by:
Sometimes when I try to close my managed C++ application, the following dialog displays in Win 2000 Pro: The title of the dialog is "Server Busy". The message is "This action cannot be completed...
2
by: Asfand Yar Qazi | last post by:
Hi, xmllint --valid gives error output on some code (included below:) file "Shotgun_Robot.dtd" (don't ask) <!ELEMENT Image (#PCDATA) > <!-- #PCDATA == image path --> <!-- x/y values...
18
by: day | last post by:
I know I've seen this issue described before, but I can't find it, or the solution now that I need it. I have some css-specified floating divs that contain images or text. The text divs have a...
1
by: blue | last post by:
Looking at code from the following: http://www.gamedev.net/reference/programming/features/enginuity2/page3.asp I'm confused though... CMMPointer(T *o) { obj=0; *this=0;
2
by: rickmorrison | last post by:
I've put up a testcase at http://forefront-tech.net/pub/badsel.html that has me baffled. The basic idea is that a small piece of Javascript adds a class name "huvr" to an element on mouseover,...
5
by: junky_fellow | last post by:
Each time i submit some pattern to "google", it shows search took 0.XX seconds for exploring millions of web pages. When i look for efficient ways of searching a string, they always say compare...
2
by: ~~~ .NET Ed ~~~ | last post by:
It is not the first time I see this happen. I am using VS.NET 2003 with .NET Framework 1.1. In this particular situation I have a custom user control in a windows form. There is a member variable...
2
by: Lasse Edsvik | last post by:
Hello I was wondering if you guys could tell me if: cmd.ExecuteReader(CommandBehavior.CloseConnection); returns a dataset, datatable or what?
15
by: David White | last post by:
The size of a struct can be affected by compiler packing. Suppose you need it to be a specific value for some reason (e.g., in firmware). How can you get the compiler to generate an error for the...
9
by: Dullme | last post by:
i can hardly know what is the code for this. I have researched palindromes, but unfortunately, I only have read about integers not for characters..I need some help..
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.