Subsection 9.9.1 FStrings
a = 5
b = 9
setStr = 'The set is {{{a}, {b}}}.'
print(setStr)
Checkpoint 9.9.4.
What is printed by the following statements?
x = 2
y = 6
print('sum of {x} and {y} is {x+y}; product: {x*y}.')
Nothing - it causes an error
It is legal format syntax: put the data in place of the braces.
sum of {x} and {y} is {x+y}; product: {x*y}.
The outpput will be the data stored in the variables, not the variables themselves.
sum of 2 and 6 is 8; product: 12.
Yes, correct substitutions!
sum of {2} and {6} is {8}; product: {12}.
The braces do not appear in the final fstring.
Checkpoint 9.9.5.
What is printed by the following statements?
v = 2.34567
print('{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!