📜  f-string 表达式部分不能包含反斜杠 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:32.240000             🧑  作者: Mango

代码示例1
This is specified in the PEP for f-strings:
Backslashes may not appear inside the expression portions of f-strings, [...]

One option is assinging '\n' to a name and then .join on that inside the f-string; that is, without using a literal:
Example:
names = ['Adam', 'Bob', 'Cyril']
nl = '\n'
text = f"Winners are:{nl}{nl.join(names)}"
print(text)