PythonMastery

Celsius to Fahrenheit program in Python

Read a temperature in Celsius, convert it with F = C × 9/5 + 32, and print the result. Also shows the Fahrenheit to Celsius version.

The formula is F = C × 9 / 5 + 32. The program reads a number, applies the formula and prints the result.

The program

python
celsius = float(input("Temperature in Celsius: "))

fahrenheit = celsius * 9 / 5 + 32

print(f"{celsius}°C is {fahrenheit}°F")

Typed in this example: 37. Press Run to type your own.

output
Temperature in Celsius: 37
37.0°C is 98.6°F

How it works

The other direction

To convert Fahrenheit to Celsius, reverse the steps: subtract 32, then multiply by 5 / 9.

python
fahrenheit = 212
celsius = (fahrenheit - 32) * 5 / 9
print(f"{fahrenheit}°F is {celsius}°C")
output
212°F is 100.0°C

Here the brackets matter: without them, Python would multiply 32 by 5 / 9 first.

Learn the idea: Input & Output: Talking to Your User, Operators & Expressions