PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Convert Celsius To Fahrenheit and Vice Versa

Python Convert Celsius To Fahrenheit and Vice Versa

Updated on: April 22, 2025 | Leave a Comment

Temperature conversion is a common task in programming, especially when dealing with scientific data, weather information, or international units. In this article, we’ll learn how to convert temperatures between Celsius and Fahrenheit using Python with examples.

Table of contents

  • Understanding Celsius and Fahrenheit
    • Conversion Formulas
  • How to Convert Celsius To Fahrenheit in Python
    • Code Example
    • Explanation
  • How to Convert Fahrenheit To Celsius in Python
    • Code Example

Understanding Celsius and Fahrenheit

Celsius and Fahrenheit are temperature scales used to measure how hot or cold something is.

  • Celsius (°C)
    • It is based on the freezing point of water at 0°C and the boiling point at 100°C.
    • Example: A normal body temperature is around 37°C.
  • Fahrenheit (°F)
    • Water freezes at 32°F and boils at 212°F.
    • Example: A normal body temperature is about 98.6°F.

Conversion Formulas

To convert between Celsius and Fahrenheit, we use these formulas:

  • Celsius to Fahrenheit: F = ( C × 9/5 ) + 32
  • Fahrenheit to Celsius: C = ( F - 32 ) × 5/9

How to Convert Celsius To Fahrenheit in Python

Steps to convert temperature from Celsius to Fahrenheit

  1. Take the Temperature in Celsius

    Get or read the temperature in degrees Celsius that you want to convert.
    For Example, the temperature = 25°C i.e., c = 25

  2. Apply the Formula

    Now put the Celsius value into the formula: F = ( C × 9/5 ) + 32
    F = ( 25 * 9/5 ) + 32 = ( 225 / 5 ) + 32 = 45 + 32 = 77°F

  3. Display the result in the proper format

    Use print() function and f-string (formatted string literal) in Python to display the result.

Code Example

c = float(input("Enter temperature in Celsius: "))
# formula to convert Celsius to Fahrenheit
f = (c * 9/5) + 32
print(f"{c}°C is equal to {f:.2f}°F")Code language: Python (python)

Output:

Enter temperature in Celsius: 37
37.0°C is equal to 98.60°F

Enter temperature in Celsius: -37
-37.0°C is equal to -34.60°F

Explanation

  • Step 1: Here, we are taking the user’s input for temperature in Celsius. As the input is in string format, we need to convert it into a float for calculation.
    • input() takes input as a string.
    • float() converts it to a decimal number (float).
    • The value is stored in the variable c.
  • Step 2: Plugging the value of c in formula: F = ( C × 9/5 ) + 32
    • f = ( 37 * 9/5 ) + 32 = ( 333 / 5 ) + 32 = 66.6 + 32 = 98.6°F.
    • The result is stored in the variable f.
  • Step 3: We use an f-string (formatted string literal) to display the result in a readable format.
    • {c} shows the input Celsius value.
    • {f:.2f} shows Fahrenheit value rounded to 2 decimal places.
    • Adds degree symbols and labels (°C, °F) for clarity.
  • Note: The Formula can be used for minus temperatures as well. For Example, if c = -37°C then f = -34.60°F

How to Convert Fahrenheit To Celsius in Python

Like the above approach, we will use the following formula to convert temperature from Fahrenheit to Celsius.

C = ( F - 32 ) × 5/9

For Example, if f = 98.6

C = ( 98.6 - 32 ) * 5/9 = ( 66.6 * 5) / 9 = 333 / 9 = 37°c.

Code Example

f = float(input("Enter temperature in Fahrenheit: "))
# formula to convert Fahrenheit To Celsius
c = (f - 32) * 5/9
print(f"{f}°F is equal to {c:.2f}°C")Code language: Python (python)

Output:

Enter temperature in Fahrenheit: 98.6
98.6°F is equal to 37.00°C

Enter temperature in Fahrenheit: -34
-34.0°F is equal to -36.67°C

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com