single/double quote escape interpolation

Discussion in 'Python' started by Simon Bunker, Jul 7, 2003.

  1. Simon Bunker

    Simon Bunker Guest

    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/
     
    Simon Bunker, Jul 7, 2003
    #1
    1. Advertising

  2. 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
     
    Skip Montanaro, Jul 7, 2003
    #2
    1. Advertising

  3. On Mon, 7 Jul 2003 14:45:42 -0500, Skip Montanaro <> 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
     
    Bengt Richter, Jul 8, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    13,243
  2. Eric Layman
    Replies:
    3
    Views:
    630
    Rad [Visual C# MVP]
    Apr 14, 2007
  3. Evan
    Replies:
    1
    Views:
    392
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  4. Evan
    Replies:
    3
    Views:
    522
    Marc 'BlackJack' Rintsch
    Nov 4, 2008
  5. vikrant
    Replies:
    8
    Views:
    366
    vikrant
    May 17, 2007
Loading...

Share This Page