string interpolation syntactic sugar

Jeremy Hylton jeremy at cnri.reston.va.us
Thu Dec 9 12:07:13 EST 1999


>>>>> "DR" == Dickon Reed <dr10009 at cl.cam.ac.uk> writes:

  DR> As far as I know, if in Python I want a string comprised of the
  DR> concatenation of some strings and some expressions converted to
  DR> strings, I have to do something like:

  DR> " a "+`x`+" b "+`y`

  DR> or:

  DR> " a %s b %s" % (`x`, `y`)

  DR> The first sometimes gets awkward and the second means I have to
  DR> match up the format string with the tuple which can be a source
  DR> of bugs.

When I have complicated formats, I usually turn to dictionaries rather
than trying to match up format strings with a tuple of values:

"a %(x)s  b %(y)s"  % { 'x': math.log(0),
                        'y': y }

I'm not sure that the suggested approach of putting some kind of
special delimiters to mark eval-able parts of strings is much of an
improvement.

Jeremy





More information about the Python-list mailing list