Python program to convert celsius to Fahrenheit and vice versa

Python program to convert celsius to Fahrenheit and Fahrenheit to celsius:

In this post, we will learn how to convert celsius to Fahrenheit in Python. The program will take the celsius value as input from the user, convert it to Fahrenheit and print it out. I will show you how to convert a celsius value to Fahrenheit and how to convert a Fahrenheit value to celsius.

How to convert a celsius value to Fahrenheit:

1 celsius is equal to 33.8 Fahrenheit. We can use the below formula to convert a celsius value to Fahrenheit:

(°C × 9/5) + 32 = °F

The program will take the celsius value as an input from the user,

Example 1: Python program to convert celsius to Fahrenheit:

The below python program converts a celsius value to Fahrenheit:

celsius = float(input("Enter the celsius value: "))
fahrenheit = celsius * (9/5) + 32

print('%0.1f°C = %0.1f°F' %(celsius, fahrenheit))

In this program,

  • It asks the user to enter the celsius value. It reads the value as float and assigned it to the celsius variable.
  • The fahrenheit value is calculated by using the above formula and assigned it to the fahrenheit variable.
  • The last statement is printing the degree celsius and converted Fahrenheit value.

It will print outputs as below:

Enter the celsius value: 22
22.0°C = 71.6°F

Enter the celsius value: 33.4
33.4°C = 92.1°F

Example 2: Python program to convert Fahrenheit value to celsius:

This program will do the opposite of the previous example. It will convert a Fahrenheit value to celsius. We can use the below formula to do the conversion:

(°F − 32) × 5/9 = °C

It is just the opposite of the previous example.

fahrenheit = float(input("Enter the fahrenheit value: "))
celsius = (fahrenheit - 32) * 5/9

print('%0.1f°F = %0.1f°C' % (fahrenheit, celsius))

It will give output as below:

Enter the fahrenheit value: 32
32.0°F = 0.0°C

Enter the fahrenheit value: 12
12.0°F = -11.1°C

Enter the fahrenheit value: 99
99.0°F = 37.2°C

Python Fahrenheit to celsius conversion

You might also like: