single/double quote escape interpolation

Bengt Richter bokr at oz.net
Tue Jul 8 04:15:38 CEST 2003


On Mon, 7 Jul 2003 14:45:42 -0500, Skip Montanaro <skip at 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__(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




More information about the Python-list mailing list