1
def fahrenheit_to_centigrade(deg_fahrenheit):
deg_c = (deg_fahrenheit - 32.)*5./9.
return deg_c

for deg_F in [-40, -30, -20, -10, 0, 10, 50, 100]:
    conv_temp = fahrenheit_to_centigrade(deg_F)
    print(f"{deg_F:f} degrees F is {conv_temp:f} degrees C")

Above is a function I have been given (intro to python so I apologise in advance if this is a terribly stupid question). I don't understand the

:f

bit in the final print statement (after deg_F and conv_temp)? What does it mean? Does it modulate the outcome so the values are of the same sig.f / class? Does this have a name so I can look it up myself?

J. Doe
  • 11
  • 1

1 Answers1

0

This is a presentation type specifier for floating-point and decimal values. 'f' means fixed precision, as opposed to e.g. scientific exponentiated notation:

Type Meaning
'e' Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent. The default precision is 6.
'E' Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.
'f' Fixed-point notation. Displays the number as a fixed-point number. The default precision is 6.
'F' Fixed-point notation. Same as 'f', but converts nan to NAN and inf to INF.
'g' General format. For a given precision p >= 1, this rounds the number to p significant digits and then formats the result in either fixed-point format or in scientific notation, depending on its magnitude.

The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.

Positive and negative infinity, positive and negative zero, and nans, are formatted as inf, -inf, 0, -0 and nan respectively, regardless of the precision.

A precision of 0 is treated as equivalent to a precision of 1. The default precision is 6.
'G' General format. Same as 'g' except switches to 'E' if the number gets too large. The representations of infinity and NaN are uppercased, too.
'n' Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters.
'%' Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign.
None Similar to 'g', except that fixed-point notation, when used, has at least one digit past the decimal point. The default precision is as high as needed to represent the particular value. The overall effect is to match the output of str() as altered by the other format modifiers.
iacob
  • 20,084
  • 6
  • 92
  • 119