Skip to main content

Section 3.9 F-Strings

In grade school quizzes a common convention is to use fill-in-the blanks. For instance,
Hello _____!
and you can fill in the name of the person greeted, and combine given text with a chosen insertion. We use this as an analogy: Python has a similar construction, called a formatted string or an f-string. An f-string makes substitutions into places in a string enclosed in braces. Run this code:
There are several new ideas here!
The string has been formatted in a new way. We have included an f before the starting quotation mark. Such a string is called an f-string. Places where braces are embedded are replaced by the value of the expression inside the braces. There are many variations on the syntax between the braces. In this case we use the syntax where the first (and only) location in the string with braces has the variable person. When this code is evaluated, the value of the person variable is placed in the string in this location.
In the code above, this new string is assigned to the identifier greeting, and then the string is printed.
The identifier greeting was introduced to break the operations into a clearer sequence of steps. However, since the value of greeting is only referenced once, it can be eliminated with the more concise version:
There can be multiple substitutions, with data of any type. Next we use floats. Try original price $2.50 with a 7% discount:
If you used the data suggested, this result is not satisfying. Prices should appear with exactly two places beyond the decimal point, but that is not the default way to display floats.
F-strings can give further information inside the braces showing how to specially format data. In particular, floats can be shown with a specific number of decimal places. For two decimal places, put :.2f inside the braces but after the variable name for the monetary values:
The 2 in the format modifier can be replaced by another integer to round to that specified number of digits.
A technical point: Since braces have special meaning in a format string, there must be a special rule if you want braces to actually be included in the final formatted string. The rule is to double the braces: { { and }}. For example mathematical set notation uses braces. The initial and final doubled braces in the format string below generate literal braces in the formatted string:
a = 5
b = 9
setStr = f'The set is {​{ {a},{b} }​}.'
print(setStr)

Checkpoint 3.9.1.

    What is printed by the following statements?
    x = 2
    y = 6
    print(f'sum of {x} and {y} is {x+y}; product: {x*y}.')
    
  • Nothing - it causes an error
  • It is legal format syntax.
  • sum of {} and {} is {}; product: {}.
  • Put the value of each expression in place of the braces.
  • sum of 2 and 6 is 8; product: 12.
  • Yes, correct!
  • sum of {2} and {6} is {8}; product: {12}.
  • Close: REPLACE the braces.

Checkpoint 3.9.2.

    What is printed by the following statements?
    v = 2.34567
    print(f'{v:.1f} {v:.2f} {v:.7f}')
    
  • 2.34567 2.34567 2.34567
  • The numbers before the f in the braces give the number of digits to display after the decimal point.
  • 2.3 2.34 2.34567
  • Close, but round to the number of digits and display the full number of digits specified.
  • 2.3 2.35 2.3456700
  • Yes, correct number of digits with rounding!
You have attempted of activities on this page.