single/double quote escape interpolation

S

Simon Bunker

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
 
S

Skip Montanaro

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
 
B

Bengt Richter

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.,
... def __getitem__(self, key): return '%(' + key + ')s'
... "why don't we drop by the %(where)s and %(dowhat)s a few?"

Maybe that was a little weird ;-) How about,
... def __getitem__(self, key):
... res = list(key.upper())
... res.reverse()
... return ''.join(res)
... "why don't we drop by the EREHW and TAHWOD a few?"
... kinds = {'where': ['pub', 'office','theatre','boathouse'],
... 'dowhat':['quaff','program','watch','expend']}
... def __getitem__(self, key):
... w = self.kinds.get(key,['??'])
... return random.choice(w)
... "why don't we drop by the theatre and quaff a few?"

Whatever ;-)

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top