Home > Enterprise >  How do you create a new line in f-strings?
How do you create a new line in f-strings?

Time:02-12

I'm trying to create new lines using python for the variable "profile" using the 'new_line' variable, but I haven't been successful.

The code below does not produce errors. It gives me all 3 strings in one line and I'd like to get 3 lines.

I would like the output to look like this with a new line for each string.

        response: response
        Lat/Lon: 1293.2312,123123.432
        City: New York"
from flask import Flask
from flask import jsonify
import requests
import json
import os


app = Flask(__name__)

@app.route('/change/')
def he():
    API_KEY = "API_KEY"
    CITY_NAME = "oakland"
    url = f"http://api.openweathermap.org/data/2.5/weather?q={CITY_NAME}&appid={API_KEY}"
    response = requests.get(url).json()
    new_line='\n'
    profile = (

        f"response: {response} ============== {new_line}"
        f"Lat/Lon: {response['coord']} ========={new_line}"
        f"City: {CITY_NAME}========{new_line}"
         
    )
   
    return profile 

CodePudding user response:

Try new_line='</br>'.

If you're viewing it in browser, it may interpret the page as badly formatted HTML and ignore line breaks and whitespaces, therefore you will need to use tags for that.

  • Related