Fall, 2020
= input("enter the temperature in Celsius: ")
C = float(C)
C = (9/5)*C + 32
F print("{0} degrees Celsius is {1} degrees Fahrenheit".format(C,F))
The print statement above uses so-called “formatted printing” to output values of the C
and F
variables to the screen. The {0}
and {1}
items are placeholders, numbered starting from 0, that tell the print
statement that values will be slotted in there, according to the list that comes in the .format()
part of the statement at the end. The C
variable gets slotted into placeholder {0}
and the F
variable gets slotted into the {1}
placeholder.
There are other ways of doing formatted printing in Python that you might come across, for example:
print("%.1f degrees Celsius is %.1f degrees Fahrenheit" % (C,F))
In the above method, the %.1f
format string is a placeholder that instructs Python to format the variable that gets slotted in, as a floating point number, using 1 decimal place.
Here is another method that is like the first one above, but using named keywords:
print("{degC} degrees Celsius is {degF} degrees Fahrenheit".format(degC = C, degF = F))
Here is a blog post describing various ways of doing formatted printing in Python: A Guide to the Newer Python String Format Techniques.