New submission from Anselm Kiefner:

Considering that

x = 3
f"{'hello' if x == 3 else 'goodbye'} world"

is a simple, elegant and powerful piece of code that works just as expected by 
itself, I often find myself stumbling and wondering why
 
f"text {'\n' if x == 3 else ''} text"

fails horribly just because of the \ within the extra quoted string that must 
be worked around with ugly code like

nl = "\n"
f"text {nl if x==3 else ''} text"

which really doesn't feel like python at all.

I am aware that the specification for f-strings says "no \ in expressions", but 
please consider that in this case, the \ is not really part of the expression 
but rather part of a string that isn't evaluated as part of the expression, 
which might just as well be referenced to by a variable.

----------
messages: 297206
nosy: Anselm Kiefner
priority: normal
severity: normal
status: open
title: Parsing error on f-string-expressions containing strings with backslash
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30793>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to