Get city temperature in Python3 with requests

In this article I will teach you step by step how to get city weather information using a free 3rd party API.

Please note that the API from openweathermap is free to use for 60 API requests or less per minute. If you exceed the limit, the API requests will no longer work until the counter resets.

Steps and requirements:

  1. Create an account on and login
  2. Get your API key from
  3. Install requests package using pip install requests to make http requests to the weather API
  4. Now start coding

First of all, you should import the requests module and store the api key that you got from the above link and store it into a variable

import requests


I will retrieve the current weather in London, UK so I will store the city name in a variable

city_name = "London"

In order to get the weather, we have to use the current weather API provided by openweathermap which is explained here:

The API url that we will use is

So we are creating the url by inserting the city name in the url and appending the API_KEY at the end of the url for authentication purposes:

If you have Python3.6 or above you can use the new f-strings syntax:

url = f"{city_name}&APPID={API_KEY}"

If you have earlier versions you can use the format syntax:

url = "{}&APPID={}".format(city_name, API_KEY)

Now we can make the API request and parse the json response as dict with the help of the json method:

response = requests.get(url).json()

Now we have all the current London weather information plus different information like city coords, timezone, sunrise and sunset time in a dict:

  "coord": {
    "lon": -0.13,
    "lat": 51.51
  "weather": [
      "id": 500,
      "main": "Rain",
      "description": "light rain",
      "icon": "10d"
  "base": "stations",
  "main": {
    "temp": 284.2,
    "pressure": 1006,
    "humidity": 93,
    "temp_min": 283.15,
    "temp_max": 285.37
  "visibility": 10000,
  "wind": {
    "speed": 5.7,
    "deg": 250
  "rain": {
    "1h": 0.25
  "clouds": {
    "all": 75
  "dt": 1571935280,
  "sys": {
    "type": 1,
    "id": 1414,
    "country": "GB",
    "sunrise": 1571899147,
    "sunset": 1571935808
  "timezone": 3600,
  "id": 2643743,
  "name": "London",
  "cod": 200

The key that interests us is "main" which stores the current temperature under the "temp" key.

But its value is odd: 284.2 and after a quick research I realized that the value is in Kelvin units and the formula to transform it to Celsius is temp_in_kelvin - 273.13 so we write a convert function which also use only the 2 decimals after the floating point:

def convert_kelvin_to_celsius(kelvin_temperature):
    return round(kelvin_temperature - 273.15, 2)

Now we are ready to get the temperature in Celsius and print it out by accessing the "temp" key from "main":

current_temperature_kelvin = response['main']['temp']

And using the convert function that we defined earlier, we will convert it to Celsius and print it:

current_temperature_celsius = convert_kelvin_to_celsius(current_temperature_kelvin)

Now if you run the python file here’s what we get:

:~$ python
:~$ 11.05

You can extend this script to retrieve the other information from the API request.

Full code below:

import requests

city_name = "London"
url = f"{city_name}&APPID={API_KEY}"

def convert_kelvin_to_celsius(kelvin_temperature):
    return round(kelvin_temperature - 273.15, 2)

response = requests.get(url).json()
current_temperature_kelvin = response['main']['temp']
current_temperature_celsius = convert_kelvin_to_celsius(current_temperature_kelvin)

5 Responses

  1. SEO Service says:

    Awesome post! Keep up the great work! 🙂

  2. Great content! Super high-quality! Keep it up! 🙂

  3. Justin says:

    Long time supporter, and thought I’d drop a comment.

    Your wordpress site is very sleek – hope you don’t mind me asking what theme you’re using?
    (and don’t mind if I steal it? :P)

    I just launched my site –also built in wordpress like yours– but the theme slows (!) the site down quite a

    In case you have a minute, you can find it by searching for
    “royal cbd” on Google (would appreciate any feedback) –
    it’s still in the works.

    Keep up the good work– and hope you all take care of yourself
    during the coronavirus scare!

  4. Wallis says:

    Hi, very nice website, cheers!
    Need cheap and reliable hosting? Our shared plans start at $10 for an year and VPS plans for $6/Mo.
    Check here:

  5. Like!! Great article post.Really thank you! Really Cool.

Leave a Reply

Your email address will not be published. Required fields are marked *