Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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_F String - Fatal编程技术网

Python:在创建f字符串之后,如何计算它

Python:在创建f字符串之后,如何计算它,python,f-string,Python,F String,我想计算从数据库查询的f字符串。这个字符串使用我在调用字符串之前定义的变量 def get_name(): name = "Ben" # from database (the value within the text field I call "name_question" in postgresql says: "My name is {name}") name_phrase = DB.objects.ge

我想计算从数据库查询的f字符串。这个字符串使用我在调用字符串之前定义的变量

def get_name():
    name = "Ben"

    # from database (the value within the text field I call "name_question" in postgresql says: "My name is {name}")
    name_phrase = DB.objects.get(phrases=name_question)
    
    print(f'{name_phrase}')
目前,我的输出是: “我的名字是{name}”

但我希望是这样 “我叫本”

我已经尝试过多种嵌套方式,包括“”、”以及
ast.literal\u eval
,但不知道如何进行嵌套。

试试这个:

print(name_phrase.format(name=name))

在创建
f-string
之后,没有特殊的函数来计算它,因此您应该使用
str.format

初始化之前如何使用
name\u短语
?请查看get函数调用的参数。@Balaji Ambresh,感谢您指出这一点,我对它进行了修改。不过,它更多的是一个伪代码为了支持“如何评估从数据库中提取的f-string”这一问题,我应该先回顾一下这一部分,谢谢。谢谢,虽然我有点失望,f-string不能像我最初打算的那样“链接”。