am having difficulty calling a variable as part of the url in a message I intend to send via the Python Telegram bot. Due to the nature of my code, the ‘webpage’ variable varies and the url has to be called as a variable. Placing the `url’ variable in curly brackets did not work and some searching online has been in vain. Appreciate any pointers.
JavaScript
x
7
1
webpage = "abc"
2
url = "https://www.example.com/" + webpage
3
message = *Website*: " + "[click here]({url})"
4
parse_mode = 'markdown'
5
url = f"https://api.telegram.org/bot{TOKEN}/sendMessage?chat_id={chat_id}&text={message}&parse_mode={parse_mode}"
6
requests.get(url).json()
7
Advertisement
Answer
Try this:
JavaScript
1
2
1
message = f"*Website*: [click here]({url})"
2
The f
before the "
causes the {url}
to be parsed, otherwise it is handled as a simple string and {}
are not getting replaced.
You can read more details about “Formatted String Literals” or simple “f-strings”, e.g. here