0

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 
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    You *are* putting newline characters in the string in the right places; the problem is that you are displaying the string in HTML, which treats newlines as just another form of whitespace. You need to insert an appropriate HTML tag, such as `
    `, instead - you could do this quite simply by changing the value assigned to `new_line`.
    – jasonharper Feb 11 '22 at 21:16
  • You can just put `\n` in the string literal directly; there's no need for `new_line`. – chepner Feb 11 '22 at 21:16
  • Thank you so much!! the '
    ' made it work.
    – Trying to Learn Javascript Feb 11 '22 at 21:20

1 Answers1

2

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.

Karashevich B.
  • 320
  • 3
  • 14