"str"(expr)"str" interpolation syntax

James J. Besemer jb at cascade-sys.com
Tue Sep 10 17:12:25 EDT 2002


I don't see that your proposals materially improve upon

    "abc" + `x*3` + "def"

which already works.

Having previously used a number of languages where string concatination 
is implied by adjacent expressions and where numeric expressions in this 
context are automatically coerced to strings, I agree that having to 
explicitly spell out "+" and `` or str() seems a BIT tedious, at least 
at first.  
However, this is a case where I have to agree that it's no big deal and 
after using it for a while you get used to it.

Regards

--jb


Beni Cherniavksy wrote:

>Hi all.
>
>I was searching for a new interpolation syntax that would use quotes
>for starting/ending the interpolated part (I don't feel comfortable
>putting code inside unclosed quotes...)
>
>After contemplating some horrors like $"""str"expr"str""" or
>$("str" expr "str"), I noticed that currently both  "(  and  )"  are
>illegal (first because strings are not callable, second is a syntax
>error).  So I propose that a parenthesized expresion near a string would
>imply concatenation.  In other words,
>	"foo"(x * 3)"bar"
>would be equivallent to
>	"foo" + str(x * 3) + "bar".
>This works of course with only one side, e.g. "foo"(x) or (y)"bar" but in
>longer cases it would be good style to add empty strings on either end,
>e.g. ""(y)"bar".  And of course it all works with single quotes too.  One
>of the benefits over current interpolation PEPs is that you can change the
>quote type in the middle...
>
>It is slightly more verbose (especially when interpolating into tripple
>quote strings: """foo"""(x)"""bar""")  but it is more readable and
>understandable (just extends the current concatenation of constant
>strings), allows clear nesting of strings into the expr:
>	"foo"('%3d' % x)"bar"
>and most importantly, you never need to remember arbitrary rules where the
>interpolation ends...
>
>What do you think?
>






More information about the Python-list mailing list