Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In Python, How do you create a new line in f-strings?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        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 


>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading