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.
4 ответов
+ 6
Use a python f string
print(f"{celsius}degrees Celsius is equal to {fahrenheit}degrees in Fahrenheit")
https://realpython.com/python-f-strings/
+ 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))
0
print(f"{celsius}degrees celsius is equal to {Fahrenheit}degrees in Fahrenheit')