Telegram Bot won't display formatted text with line breaks

I’m building a Telegram bot that should respond to the /rates command with nicely formatted data. Right now when someone types the command, I want the bot to reply with something like this:

Last Update: data[timestamp]
Buy Rate: data[buy_price]
Sell Rate: data[sell_price]
Change: data[change_amount]

But I’m having two main problems. First, I can’t get the descriptive text to show up properly. Second, everything comes out as one long line instead of being split into separate lines like I want.

I’ve tried different approaches but nothing works. I’m pretty new to Python so maybe I’m missing something obvious.

from telegram.ext import Updater, CommandHandler
import requests

def fetch_rates():
    response = requests.get('https://api.example.com/currency/usd/info').json()
    timestamp = response['timestamp']
    buy_price = response['buy_price']
    sell_price = response['sell_price']
    change_amount = response['change_amount']
    rates_info = (timestamp + buy_price + sell_price + change_amount)
    return rates_info

def handle_rates(bot, update):
    rates_data = fetch_rates()
    chat_id = update.message.chat_id
    bot.send_message(chat_id=chat_id, text=rates_data)

def main():
    updater = Updater('TOKEN_HERE')
    dispatcher = updater.dispatcher
    dispatcher.add_handler(CommandHandler('rates', handle_rates))
    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()

Any ideas what I’m doing wrong?

Your fetch_rates() function is just mashing strings together without any formatting. When you do timestamp + buy_price + sell_price + change_amount, you’re literally joining values with no labels or line breaks. You need to format the string properly. Replace your rates_info line with: rates_info = f"Last Update: {timestamp}\nBuy Rate: {buy_price}\nSell Rate: {sell_price}\nChange: {change_amount}". The \n adds line breaks, and f-string formatting lets you include labels with your data. I made the same mistake when I started with Telegram bots - forgot that concatenation doesn’t add any formatting.

Your string concatenation is the problem. You’re mixing data types from the JSON response - some variables might be numbers, not strings. This throws a TypeError when you try to concatenate them. Fix it by converting everything to strings first with str(). Also, your current code creates one messy string with no separators. Try this instead: rates_info = f"Last Update: {str(timestamp)}\nBuy Rate: {str(buy_price)}\nSell Rate: {str(sell_price)}\nChange: {str(change_amount)}". The str() calls prevent type errors and the \n characters format your output nicely. I’ve hit this exact issue before with API responses that return mixed data types.

you’re just mashing raw values together without any structure. the problem is you’re treating everything like strings, but api responses usually have mixed data types. use the format() method or build it piece by piece: rates_info = "Last Update: " + str(timestamp) + "\n" + "Buy Rate: " + str(buy_price) and so on. sometimes this works better than f-strings when you’re dealing with messy api data.