473,667 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

single/double quote escape interpolation

I was just wondering why python doesn't make a distinction between single
and double
quotes - a bit like Perl does.

Obviously I realise there are no dollar signs so you can't intrpolate a
varaible in a string.
This is fine, but having to remember to put an r in front of regex's is
really annoying and it would
be great if you could jsut use single quotes instead to interpolate slashes
properly etc. (ie only escape them once).

I could not find this on google, but I guess it has been discussed before.
Is there a good reason?

thanks

Simon
--
http://www.rendermania.com/
Jul 18 '05 #1
2 4758

Simon> I was just wondering why python doesn't make a distinction
Simon> between single and double quotes - a bit like Perl does.

In most instances it's helpful because you can avoid extra escapes, e.g.:

"why don't we drop by the pub and quaff a few?"

instead of

'why don\'t we drop by the pub and quaff a few?'

There are also triple-quoted strings using """ and ''' as the string
delimiters. They mostly just make it easy to create multi-line string
literals, but they can also be used to avoid backslashes:

'''Maury said, "Why don't we drop by the pub and quaff a few?"'''

Simon> Obviously I realise there are no dollar signs so you can't
Simon> intrpolate a varaible in a string.

You can interpret variables, the mechanism is just slightly different:

"why don't we drop by the $where and $dowhat a few?"

for Perl, vs.

"why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict

for Python. Somedict is a dictionary having keys "where" and "dowhat" (at
minimum). The most common "somedict"s are probably "locals()" and
"globals()" though you can easily construct your own or take them from
different contexts, like SQL query results.

Simon> This is fine, but having to remember to put an r in front of
Simon> regex's is really annoying and it would be great if you could
Simon> jsut use single quotes instead to interpolate slashes properly
Simon> etc. (ie only escape them once).

I find having to run to the Camel book every other day more annoying. ;-) I
can never remember what all the qx, qw, etc stuff means.

Skip

Jul 18 '05 #2
On Mon, 7 Jul 2003 14:45:42 -0500, Skip Montanaro <sk**@pobox.com > wrote:

Simon> I was just wondering why python doesn't make a distinction
Simon> between single and double quotes - a bit like Perl does.

In most instances it's helpful because you can avoid extra escapes, e.g.:

"why don't we drop by the pub and quaff a few?"

instead of

'why don\'t we drop by the pub and quaff a few?'

There are also triple-quoted strings using """ and ''' as the string
delimiters. They mostly just make it easy to create multi-line string
literals, but they can also be used to avoid backslashes:

'''Maury said, "Why don't we drop by the pub and quaff a few?"'''

Simon> Obviously I realise there are no dollar signs so you can't
Simon> intrpolate a varaible in a string.

You can interpret variables, the mechanism is just slightly different:

"why don't we drop by the $where and $dowhat a few?"

for Perl, vs.

"why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict

for Python. Somedict is a dictionary having keys "where" and "dowhat" (at
minimum). The most common "somedict"s are probably "locals()" and
"globals()" though you can easily construct your own or take them from
different contexts, like SQL query results.

Might want to mention that somedict only has to act like a dict, not necessarily *be*
a dict. I.e., supporting __getitem__ suffices, so you can synthesize anything you like
from the key passed. E.g.,
class AsIs(object): ... def __getitem__(sel f, key): return '%(' + key + ')s'
... somedict = AsIs()
"why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the %(where)s and %(dowhat)s a few?"

Maybe that was a little weird ;-) How about,
class RU(object): ... def __getitem__(sel f, key):
... res = list(key.upper( ))
... res.reverse()
... return ''.join(res)
... somedict = RU()
"why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the EREHW and TAHWOD a few?"
class RanWord(object) : ... kinds = {'where': ['pub', 'office','theat re','boathouse'],
... 'dowhat':['quaff','progra m','watch','exp end']}
... def __getitem__(sel f, key):
... w = self.kinds.get( key,['??'])
... return random.choice(w )
... import random
somedict = RanWord()
"why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the pub and expend a few?" "why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the boathouse and quaff a few?" "why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the office and expend a few?" "why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the boathouse and program a few?" "why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the office and program a few?" "why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the pub and expend a few?" "why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the pub and program a few?" "why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict "why don't we drop by the boathouse and watch a few?" "why don't we drop by the %(where)s and %(dowhat)s a few?" % somedict

"why don't we drop by the theatre and quaff a few?"

Whatever ;-)

Regards,
Bengt Richter
Jul 18 '05 #3

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

Similar topics

5
8259
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to understand.)
9
21429
by: Hugo Coolens | last post by:
I need to use the str_replace function for a php-script which works together with html/javascript. In the script I have to perform things like: $arabic=str_replace ("'","\'", $row); $arabic=str_replace ("sh", "\š", $row); the second line works perfectly, however I can't make the first one work, it should replace a single quote with a quote preceded by a backslash. I presume I have to escape some characters to make this happen however...
7
22766
by: Leif B. Kristensen | last post by:
I'm working with a Python program to insert / update textual data into a PostgreSQL database. The text has single and double quotes in it, and I wonder: What is the easiest way to escape quotes in Python, similar to the Perlism "$str =~ s/()/\\$1/g;"? I tried the re.escape() method, but it escapes far too much, including spaces and accented characters. I only want to escape single and double quotes, everything else should be acceptable...
2
14444
by: girish | last post by:
In my XML document, some node attributes data contains both single quot and double quote characters, such as <input msg="Hello "World", What's up"/>. The double quotes are in form of escape sequence in the XML document. am not able to locate such elements using an XPath expression. I trie the following: //*
4
10626
by: sankofa | last post by:
hi, i can't seem to be able to escape my single quote properly... is it even possible in javascript? this is a portion of my code.. var DLEWIS="Pastor Lewis"; .... Sermon is a yser-defined class .. var en_20031102=new Sermon("11/02",DLEWIS,"The Lord\'s Supper: The Art Of
2
4234
by: Diarmaid McGleenan | last post by:
Hi all. This isn't quite the same single/double-quote problem we see posted a multitude of times in this ng. Read on... I have a js function called ShowActiveLink which accepts a string containing the full "<a href...>" element. My problem is that some of the hrefs contain both single and double quotes and I don't have the ability to escape these characters manually because our content management system inserts the href when it generates...
5
11448
by: Joel | last post by:
Hi, I incorporated a function in my code that whenever I use a string variable in an sql statement if the string contains a single quote it will encase it in double quotes else single quotes. Queston: How do you handle a string that contains both single & double quotes (i.e. 12'X7") Here's the function:
4
31222
by: Greg | last post by:
I keep getting an error when I have a tick mark in a text value that I am searching for in my XPath Query. Example: <Authors> <Author LastName="O'Donnel"> <Author LastName="Smith"> </Authors>
15
4462
by: bill | last post by:
I am trying to write clean code but keep having trouble deciding when to quote an array index and when not to. sometimes when I quote an array index inside of double quotes I get an error about enased whitespace (to my best memory) AT other times I get an undefined index notice as below: Notice: Undefined index: last_reminder_id in...
0
8458
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
8366
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
8790
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
8650
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
6206
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
4202
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...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2779
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
2017
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.