473,809 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

raw strings and \

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
20 2270
pl****@alumni.c altech.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.blogs pot.com

Mar 6 '06 #11
>Alas, somebody will now quote Emerson at you, I fear;-).

Let them come :-)

I almost always see this (mis)quoted as:

"consistenc y 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
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
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 unpredictabilit y 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
"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
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
pl****@alumni.c altech.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
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
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
John Salerno <jo******@NOSPA Mgmail.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

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

Similar topics

20
5783
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT" >>>c = extract(a,b) >>>print c "abcdefghijklmnop"
17
7405
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently interchangeable in all circumstances (as long as they're paired) so there's no overloading to muddy the language. Of course there could be some interesting problems with current code that doesn't make a distinction, but it would be dead easy to fix...
16
2441
by: Paul Prescod | last post by:
I skimmed the tutorial and something alarmed me. "Strings are a powerful data type in Prothon. Unlike many languages, they can be of unlimited size (constrained only by memory size) and can hold any arbitrary data, even binary data such as photos and movies.They are of course also good for their traditional role of storing and manipulating text." This view of strings is about a decade out of date with modern programmimg practice. From...
4
10543
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three sets of character strings from the user. Then I want to run through each element and compare the two strings. If they match I print they match... I'm having a bit of trouble with the actual loop through each array using the pointers and comparing...
25
3581
by: Rainmaker | last post by:
Hi, Can anyone tell me an efficient algorithm to sort an array of strings? Keep in mind that this array is HUGE and so the algorithm should me efficient enough to deal with it. Thanks
6
1764
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an character array that you must use the strcpy() function?
2
22615
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the amount and size of strings in the array won't be known until run time (in the example I have it in...
19
3119
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html contains the following data: Bladefist-Horde Nordrassil-Horde Draenor-Alliance Nordrassil-Alliance Nordrassil-Neutral Draenor-Horde
95
5448
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ____________________________________ | | | ------------------ | | | BUTTON | | | ...
0
9721
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
9600
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
10633
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
10376
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...
0
10114
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...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.