[Python-ideas] Briefer string format

Ben Finney ben+python at benfinney.id.au
Fri Jul 24 22:27:34 CEST 2015

Greg Ewing <greg.ewing at canterbury.ac.nz> writes:

> Steven D'Aprano wrote:
> > I don't think I want this behaviour:
> >
> >     f'{spam}' '{eggs}'
> >     => format(spam) + '{eggs}'
> What do you think should happen in this case:
>    '{spam}' f'{eggs}'
> It would seem very strange to me if the f infected strings *before* it
> as well as after it.

The existing behaviour of implicit concatenation doesn't give much of a
guide here, unfortunately::

    >>> 'foo\abar' r'lorem\tipsum' 'wibble\bwobble'

    >>> type(b'abc' 'def' b'ghi')
      File "<stdin>", line 1
    SyntaxError: cannot mix bytes and nonbytes literals

So, the ‘b’ prefix expects to apply to all the implicitly-concatenated
parts (and fails if they're not all bytes strings); the ‘r’ prefix
expects to apply only to the one fragment, leaving others alone.

Is the proposed ‘f’ prefix, on a fragment in implicit concatenation,
meant to have behaviour analogous to the ‘r’ prefix or the ‘b’ prefix,
or something else? What's the argument in favour of that choice?

 \        “If we ruin the Earth, there is no place else to go. This is |
  `\    not a disposable world, and we are not yet able to re-engineer |
_o__)                      other planets.” —Carl Sagan, _Cosmos_, 1980 |
Ben Finney

More information about the Python-ideas mailing list