解析 context.bot.send_message() 中的变量

Parsing variables in context.bot.send_message()

我目前正在 python-telegram-bot 的帮助下开发一个机器人,但我遇到了一些问题。

看到的目标是,当用户运行命令 /cassosspain 时,机器人会回答一个先前从数据库中选择并保存在变量中的数字。让我们称之为 var

var = "12.321.231"

def casosspain(update, context):
context.bot.send_message(
    chat_id=update.effective_chat.id,
    text="Los casos confirmados en españa son: "
)

我在 text="" 声明之后尝试使用 print(var) 但它不起作用,因为它是一个位置参数在关键字参数之后。

我的目标是打印出这一行:

Los casos confirmados en españa son: 12.321.231

关于如何使这项工作有任何想法吗?非常感谢您的帮助

您可以使用 python 的 f 字符串 7.1.1. Formatted String Literals

text = f"Los casos confirmados en españa son: {var}"
>>> var = "12.321.231"
>>> text = f"Los casos confirmados en españa son: {var}"
>>> print(text)
Los casos confirmados en españa son: 12.321.231