f-string syntax deficiency?

Chris Angelico rosuav at gmail.com
Tue Jun 6 10:08:04 EDT 2023


On Wed, 7 Jun 2023 at 00:06, Neal Becker <ndbecker2 at gmail.com> wrote:
>
> The following f-string does not parse and gives syntax error on 3.11.3:
>
> f'thruput/{"user" if opt.return else "cell"} vs. elevation\n'
>
> However this expression, which is similar does parse correctly:
>
> f'thruput/{"user" if True else "cell"} vs. elevation\n'
>
> I don't see any workaround.  Parenthesizing doesn't help:
>  f'thruput/{"user" if (opt.return) else "cell"} vs. elevation\n'
>
> also gives a syntax error

Is this a problem with the f-string, or with the expression
opt.return? That's a keyword.

ChrisA


More information about the Python-list mailing list