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