Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > single/double quote escape interpolation

Reply
Thread Tools

single/double quote escape interpolation

 
 
Simon Bunker
Guest
Posts: n/a
 
      07-07-2003
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/


 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      07-07-2003

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

 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      07-08-2003
On Mon, 7 Jul 2003 14:45:42 -0500, Skip Montanaro <(E-Mail Removed)> 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__(self, 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__(self, 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','theatre','boathouse'],
... 'dowhat':['quaff','program','watch','expend']}
... def __getitem__(self, 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid on load; replace all double single quote to single quote to display to user Eric Layman ASP .Net 3 04-14-2007 07:16 AM
replacing strange quote with standard quote Stimp ASP .Net 2 09-20-2006 10:01 AM
Single Quote Versus Double Quote In A href link knee-dragger@hotmail.com HTML 3 06-13-2006 12:42 AM
Assignment of var without escape/interpolation Bart Van der Donck Perl Misc 16 04-17-2006 10:29 AM
How to stop interpolation of Escape character in variable? \Rob\ ASP .Net 8 02-24-2006 08:52 AM



Advertisments