[Python-ideas] String interpolation for all literal strings

Ron Adam ron3200 at gmail.com
Fri Aug 7 22:42:26 CEST 2015



On 08/07/2015 08:18 AM, Nick Coghlan wrote:
> With the format string containing indices rather than the original
> expressions, we'd still want to pass in the text of those as another
> tuple, though.
>
> With that signature the default interpolator would look like:
>
>      def __interpolate__(format_str, expressions, values):
>          return format_str.format(*values)

While reading this discussion, I was thinking of what it would like if it 
was reduced to a minimal pattern that would still resemble the concept 
being discussed without any magic.  To do that, each part could be handled 
separately.

       def _(value, fmt=''):
           ('{:%s}' % fmt).format(value)

And then the exprssion become the very non-magical and obvious...

       'abc' + _(expr1) + 'def' + _(expr2) + 'ghi'

It nearly mirrors the proposed f-strings in how it reads.

        f"abc{expr1}def{expr2}ghi"

Yes, it's a bit longer, but I thought it was interesting.  It would also be 
easy to explain.  There aren't any format specifiers in this example, but 
if they were present, they would be in the same order as you would see them 
in a format string.

Cheers,
    Ron













More information about the Python-ideas mailing list