[Python-ideas] Briefer string format

Ron Adam ron3200 at gmail.com
Thu Jul 23 19:52:30 CEST 2015



On 07/23/2015 10:22 AM, Steven D'Aprano wrote:
>
> (3) The hard case, when you mix f and non-f strings.
>
>      f'{spam}' '{eggs}'
>
> Notwithstanding raw strings, the behaviour which makes sense to me is
> that the implicit string concatenation occurs first, followed by format.
> So, semantically, if the parser sees the above, it should concat the
> string:
>
>      => f'{spam}{eggs}'
>
> then transform it to a call to format:
>
>      => format(spam) + format(eggs)

I think this should be...

        => f'{spam}{{eggs}}'

The advantage that has is you could call it's format method manually again 
to set the eggs name in a different context.


It would also work as expected in the case the second stirng is a f-string.

        '{spam}' f'{eggs}'

        f'{{spam}}{eggs}'


So if any part of an implicitly concatenated string is an f-string, then 
the whole becomes an f-string, and the parts that were  not have their 
braces escaped.


The part that bothers me is it seems like the "f" should be a unary 
operator rather than a string prefix.

As a prefix:

       s = f'{spam}{{eggs}}'        # spam
       s2 = s.format(eggs=eggs)     # eggs


As an unary operator:

       s  = ? '{spam}{{eggs}}'    # spam
       s2 = ? s                   # eggs

(? == some to be determined symbol)

They are just normal strings in the second case.


Cheers,
    Ron








More information about the Python-ideas mailing list