string interpolation syntactic sugar

Skip Montanaro skip at mojam.com
Thu Dec 9 12:22:37 EST 1999


>>>>> "Jeremy" == Jeremy Hylton <jeremy at cnri.reston.va.us> writes:

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

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

It's perhaps worth pointing out that in situations where all the items you
want to interpolate are local variables you can use the locals() builtin
function instead of building a dict on-the-fly in your code

    "a %(x)s  b %(y)s" % locals()

(I know Jeremy knows this, just pointing it out for others new to Python's
string interpolation facility.)

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list