What is the easiest way to print variable within a string in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the easiest way to print variable within a string in python?

Here is a snippet of a small code I recently made to convert Celsius to Fahrenheit: print(celsius,"degrees Celsius is equal to ", fahrenheit,"degrees in Fahrenheit") This is how I have been printing variables within strings, but I was wondering if there was a better method. Even If there is no "better" method, I would love to learn how to do this in diffrent ways.

7th May 2020, 6:50 PM
Amy CruxCraft
Amy CruxCraft - avatar
3 Answers
+ 6
Use a python f string print(f"{celsius}degrees Celsius is equal to {fahrenheit}degrees in Fahrenheit") https://realpython.com/python-f-strings/
7th May 2020, 7:06 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
You can convert your integer values to strings and concatenate them. Will be longer though. print(str(Celsius)+"degrees Celsius is equal to " + str(Fahrenheit))
7th May 2020, 6:55 PM
Justus
Justus - avatar
0
print(f"{celsius}degrees celsius is equal to {Fahrenheit}degrees in Fahrenheit')
13th May 2020, 4:03 AM
Aayush Joshi
Aayush Joshi - avatar