This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Backslashes in function arguments in f-string expressions
Type: behavior Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, nabelekt
Priority: normal Keywords:

Created on 2021-02-15 00:52 by nabelekt, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg386978 - (view) Author: Thomas Nabelek (nabelekt) Date: 2021-02-15 00:52
From https://www.python.org/dev/peps/pep-0498/#escape-sequences:
"Backslashes may not appear inside the expression portions of f-strings"

Is this supposed to be true even for arguments to functions inside of the expression?

my_str = "test\ test\ test"
path = f"{my_str.replace(r'\ ', ' ')}"

gives

my_str = "test\ test\ test"
path = f"{my_str.replace(r'\ ', ' ')}"

SyntaxError: f-string expression part cannot include a backslash
msg386979 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-02-15 01:00
Yes, they're prohibited anywhere inside of the braces {}.

This might be relaxed in the future, but through 3.9 it's still enforced.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87393
2021-02-15 02:24:34eric.smithsetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-02-15 01:00:22eric.smithsetmessages: + msg386979
2021-02-15 00:57:57xtreaksetnosy: + eric.smith
2021-02-15 00:52:28nabelektcreate