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