473,785 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1859
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...sometime s 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.net wrote:
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

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

Similar topics

6
16958
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 because the other program is busy. Choose 'Switch to' to activate the busy program and correct the problem." I don't know why this is displayed. I would prefer to disable the display of this message if possible. My app needs to be able to...
2
2380
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 default to 0, w/h values default to image w/h--> <!ATTLIST Image
18
5727
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 background color and text color that differ from the rest of the page, and I'd like them to be positioned from the right edge of the rightmost image div, to the right edge of the browser window. In other words, if the browser window is 800 pixels...
1
2233
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
1395
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, and then removes it on mouseout. CSS for the "huvr" class sets various display effects. Pretty basic DHTML technique. This all seems to work fine in Gecko (and I'm assuming in other standards-based engines). It breaks in strange ways in IE6.
5
2078
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 your string with the strings in the file one by one. if there are millions of web pages then these algorithms would take considerable amount of time. So, what does google do for searching a pattern. Does it use thousands of parallel computers...
2
2034
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 declared with the fully qualified class name (some.namespace.mycontrolclass). Then in the InitializeComponent() it is instantiated and there is a block of code lines that set the various properties of the control class, then further down the usual...
2
1491
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
2035
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 wrong size rather than assert it at run-time? Here is one way, but I don't know if it's guaranteed to work on any compiler: 1/(sizeof(struct my_struct) == correct_size); For me, the above produces a compile-time divide-by-zero error for the wrong...
9
4027
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
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.