string interpolation for python

Laurent Claessens moky.math at gmail.com
Mon Apr 2 09:02:19 EDT 2012


> Seems you miss understood my notion of dynamic string.
> Dynamic strings are expressions in disguise: the things
> in between $...$ are plain old expressions (with optional
> formatting specifications). They are evaluated
> as if they were outside the dynamic string. We put them
> in there to to kill two birds with one stone:
>     1) ease of reading;
>     2) place holding.


like this one ?

b = dict(name="Sue", job="SAS sharp-shooter")
print "$b['name']$ works as b['job']"

Is it really easier to read that the following ?
"{0} works as {1}".format(b['name'],b['job'])

In the case in which b is an object having "job" and "name" attribute, 
the dynamic string will write

"$b.name$ works as $b.job$"
instead of
"{0}.name works as {0}.job".format(b)

Laurent



More information about the Python-list mailing list