Search code examples
pythonpython-3.xtelegramtelegram-botpy-telegram-bot-api

Parsing variables in context.bot.send_message()


I'm currently developing a bot with the help of python-telegram-bot and I've encountered myself with a bit of a problem.

See the goal is that when the user runs the command /casosspain the bots answers with a number which previously selects from a database and saves on a variable. Lets call it 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: "
)

I tried with print(var) after the text="" declaration but it doesn't work since its a positional argument after a keyword argument.

My goal is to print out this line:

Los casos confirmados en españa son: 12.321.231

Any ideas on how could I make that work? Would really appreciate the help


Solution

  • You can use python's f strings 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