[Python-ideas] Briefer string format

Chris Angelico rosuav at gmail.com
Thu Jul 23 05:41:15 CEST 2015


On Thu, Jul 23, 2015 at 1:31 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> I would go further and allow all the f prefixes apart from the first to
> be optional. To put it another way, the first f prefix "infects" all the
> other string fragments:
>
> msg = (f'a long message here blah blah {x}'
>         ' and {y} and {z} and more {stuff} and {things}'
>         ' and perhaps even more {poppycock}'
>        )
>
> should be exactly the same as the first version.

Editors/IDEs would have to be taught about this (and particularly,
taught to show f"..." very differently from "..."), because otherwise
a missed comma could be very surprising:

lines = [
    "simple string",
    "string {with} {braces}",
    f"formatted string {using} {interpolation}" # oops, missed a comma
    "another string {with} {braces}"
]

Could be a pain to try to debug that one, partly because stuff is
happening at compile time, so you can't even pretty-print lines
immediately after assignment to notice that there are only three
elements rather than four.

That solved, though, I think you're probably right about the f prefix
infecting the remaining fragments. It'd be more consistent to have it
*not* infect, the same way that an r or u/b prefix doesn't infect
subsequent snippets; but if anyone's bothered by it, they can always
stick in a few plus signs.

ChrisA


More information about the Python-ideas mailing list