Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我不能用字典格式化f字符串?_Python_Dictionary_F String - Fatal编程技术网

Python 为什么我不能用字典格式化f字符串?

Python 为什么我不能用字典格式化f字符串?,python,dictionary,f-string,Python,Dictionary,F String,试图在python中使用f string打印出这个格式化字符串,但它不起作用。您已经切换了]和“在最后两个代码块中,应该是这样的 同时也要避免对可变名称使用dict dict = {"name": "Shah", "birth-date": 1998, "birth_location": "New York City", "major": "computer scie

试图在python中使用f string打印出这个格式化字符串,但它不起作用。

您已经切换了]和“在最后两个代码块中,应该是这样的

同时也要避免对可变名称使用dict

dict = {"name": "Shah", "birth-date": 1998, "birth_location": "New York City", "major": "computer science"

print(f"{dict['name']} is a {dict['major']} who was born in {dict['birth-location]'}. He was born in {dict['birth-location]'}")


产出:
沙阿是一名计算机科学家,生于1998年。他出生在纽约市

有什么不对?您是否确实将dict重写为变量名?这不是一个好主意。Shah是您之前定义的变量吗?您必须了解更多关于“不工作”的含义,您选择不包含该信息的原因是什么?@mapf现在已修复,Shah是一个变量的值key@ForceBrushah是键的变量为什么不``dict={name:shah bhuiyan,出生地点:纽约,出生日期:1998}printf{dict['name']出生于{dict}['birth-location']出生于{dict}['birth-location']}“工作因为你的钥匙是出生地点,而不是出生地点看问题的顶部问:出生地点:纽约市,在最后一个问题中,你在打印声明的末尾缺少结束语,而且名字应该在引号中dict={name:shah bhuiyanwell现在看看我的东西,请兄弟dict={姓名:shah bhuiyan,出生地点:纽约,出生日期:1998}printf{dict['name']}出生于{dict['birth-location']}出生于{dict['birth-date']}请帮助我

d = {"name": "Shah", "birth-date": 1998, "birth_location": "New York City", "major": "computer science"}

print(f"{d['name']} is a {d['major']} who was born in {d['birth-date']}. He was born in {d['birth_location']}")