当下有这么一个场景:
username = '川建国'
print(f"名字:{username}, 我爱{我的国}")
但是 Python 一直在报 "我的国"没有定义,并且"我的国"前后的{}还必须要带着。无奈只能使用 % 了。
请教各位大佬有没有解决方法。
1
Trim21 2020 年 6 月 9 日 via Android
f""+""
|
2
weyou 2020 年 6 月 9 日
print(f"名字:{username}, 我爱{{我的国}}")
|
3
Drapor 2020 年 6 月 9 日
```
print(f"名字:{username}, 我爱{{我的国}}") ``` |
4
kokutou 2020 年 6 月 9 日
特殊符号要转义。。
|
5
swulling 2020 年 6 月 9 日 via iPhone
两个大括号括起来就可以了
|
6
yeyu123 2020 年 6 月 9 日
f 里面 {}中是个变量. 第一个 username =川建国, 第二个 我的锅=?
|
7
zdnyp 2020 年 6 月 9 日
我的国确实没定义啊。
username = '川建国' myworld = '中国' print(f"名字:{username}, 我爱{myworld}") 或者 print(f"名字:{username}, 我爱{'我的国'}") |
10
no1xsyzy 2020 年 6 月 9 日
https://www.v2ex.com/t/671782#r_8971898
…… 你可以续行混合 f-string 和一般的 string username = '川建国' print(f"名字:{username}, " "我爱{我的国}") |
11
qW7bo2FbzbC0 2020 年 6 月 9 日
```
print(f"名字:{username}, 我爱{{我的国}}") ``` c# 也是如此 |