python - Python:如何在创建 f 字符串后对其进行评估
问题描述
我想评估从数据库中查询的 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_eval
但不知道该怎么做。
解决方案
试试这个:
print(name_phrase.format(name=name))
创建后没有用于评估an 的特殊功能,f-string
因此您应该使用str.format
.
推荐阅读
- scala - 与 BigDecimal 相乘
- architecture - REP和CCP的区别
- python - 如何使用正则表达式查找所有以冒号 (:) 结尾的单词
- symfony - Symfony 5:如何在没有表单的情况下使用 ajax 创建(在数据库中)
- javascript - Vue Devtools 未使用 Vue Electron Builder 加载
- python - datetime.strptime 和从 dateutil 解析之间的区别?
- c - 将矩阵表示为特定范围内的唯一标量数
- python - 为什么我在导入 win32.client 时出现属性错误
- javascript - create-react-app 命令最后失败,errno -4058
- windows - TCL\Expect:: spawn ssh 显示“系统找不到指定的文件”错误