top of page

Convert Fahrenheit to Celsius (Python)

Updated: Mar 29, 2023

How to Use Python to Convert Fahrenheit to Celsius


Temperature conversion is the process of changing the value of temperature from one unit to another.

In this tutorial, we'll walk you through the steps to create a short Python script that converts Fahrenheit temperature values to Celsius. This is a great task for beginners, as it involves basic arithmetic and variable assignments.


ree

Step 1: Set up the initial variables

  • The first step is to define two variables: fahrenheit and celsius. In this example, Fahrenheit will be set to 68, which is a typical room temperature in Fahrenheit. We'll need to define celsius as well, but initially set it with a value of 0.


# Define the initial variables
fahrenheit = 68
celsius = 0

Step 2: Use the conversion formula

  • To convert Fahrenheit to Celsius, we'll use the following formula:


C = (F - 32) * 5/9

Where C is the temperature in Celsius, and F is the temperature in Fahrenheit. To use this formula in Python, we'll need to assign the result of the formula to the celsius variable.


# Use the conversion formula
celsius = (fahrenheit - 32) * 5/9

Step 3: Print the result

  • Finally, we'll want to print the result of our conversion to the console. To achieve this, we'll use the print() function, which takes a string argument that we'll format using an f-string.


# Print the result
print(f"{fahrenheit} degrees Fahrenheit is equal to {celsius:.1f} degrees Celsius.")

The :.1f syntax in the f-string ensures that the result is formatted to one decimal place.


Step 4: Run the script


We've now completed our simple Python script to convert Fahrenheit to Celsius. Save the script as a .py file and run it using a Python Interpreter. If everything was done correctly, you should see the following output:


68 degrees Fahrenheit is equal to 20.0 degrees Celsius.

Congratulations! You've successfully written a short Python script that converts Fahrenheit to Celsius.


- Erik-Rai

Comments


bottom of page