string interpolation syntactic sugar

Dickon Reed dr10009 at cl.cam.ac.uk
Thu Dec 9 10:02:58 EST 1999


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

" a "+`x`+" b "+`y`

or:

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

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

I propose a new kind of string literal wherein bracketed expressions
are evaluated then converted to strings. A syntax might be that the
string must be prefixed with an e and that curly braces should be used
for bracketing, but this the details obviously could vary. So I would
write the above case as:

e" a {x} b {y}"

Backslashifying the curly braces would have the obvious effect.

Background: I've used Python a fair bit for the last few years, but
had to learn Perl recently, and am starting to miss Perl's string
interpolation. But, Perl's syntax won't really work verbatim because
Python's variable names aren't prefixed. And this proposal is more
powerful because you get things like e"The square root of 5 is
{math.sqrt(5.0)}", or nest it should you be that way inclined- eg 
e"lots of numbers{ e" {x} "*y}". I wouldn't advocate nesting it, but at
least nothing obvious breaks. I also think that, for non C programmers
at least, writing

e" {hex(x)} "

might be more intuitive than:

" %x " % x

I'd imagine that it might be fairly efficient to implement, because the
e" a {x} b {y}" could perhaps be transformed in the bytecode generator
in to the same resulting bytecode as " a "+`x`+" b"+`y`.

An alternative approach might have a function to do this:

interpolate("a {x} b {y}")

I wonder if this could be implemented as a library service (if it
hasn't already been done). Is it as simple as chopping up the string
and calling str(eval(foo)) on each bracketted component? The place I
come unstuck with this is obtaining the globals and locals of the
caller so that the expressions are evaluated in the environment where
interpolate is called, but perhaps I'm missing something. In any case,
implementing such a routine as a Python function is probably
suboptimal because then the string would have to be lexed every time
the expression containg the interpolate was evaluated. I think it
would be better for the lexing to be done only once, and preferably at
byte code compilation time.

Comments?

Dickon Reed









More information about the Python-list mailing list