Build a Text Report
Keep numerical values separate from text, then build a readable temperature report with strings and an f-string. Choose one decimal place without changing the stored mean.
The calculation produces numbers. A report also needs text that tells a reader what those numbers mean.
Quotation Marks Create Text
Python writes text values, called strings, between matching quotation marks:
The quotation marks mark the beginning and end of each string. They are part of the source code, not part of the stored text.
A number and quoted digits are different values:
Both may display as 4, but count is a number that can take part in
arithmetic. count_text is one character of text.
Q1. Distinguish a number from text
Which instruction stores text rather than a numerical value?
Select one choice, then check.
HintFind the quotation marks
Python string literals are written inside matching quotes.
SolutionThe quoted 18 is text
"18" is a string. The other two instructions produce the integer 18.
Build Text from Smaller Parts
The + operator joins strings:
label = "Mean" + ": "
The result is the text Mean: . Python does not insert a space on its own; the
space after the colon is written inside the second string.
Joining text this way becomes awkward when the report contains a number.
Python does not allow "Mean: " + mean because a string and a number are
different kinds of value. An f-string places expressions inside a string:
print(f"Mean: {mean} {unit}")
The letter f before the opening quote tells Python to evaluate the expressions
inside braces. If mean is 20.5 and unit is "°C", the output is:
Mean: 20.5 °C
Build the named report
Run the complete calculation and inspect how the f-string combines text with numerical values.
Ready to run.
Choose the Display Precision
An f-string can state how a number should appear. In {mean:.1f}, .1f asks
for one digit after the decimal point:
print(f"Mean: {mean:.1f} {unit}")
For this program the output remains Mean: 20.5 °C. A mean of 20.56 would
appear as 20.6. The formatting changes the displayed text; it does not replace
the numerical value stored under mean.
Q2. Complete the report line
Which instruction displays Count: 4 readings when count is 4?
Select one choice, then check.
HintLook inside the braces
An f-string evaluates expressions written between { and }.
SolutionUse an f-string
print(f"Count: {count} readings") combines the text with the current
numerical value of count.
Q3. Write a one-line report
Complete the final line so that the program displays Total: 82 °C.
Editable Python
Ready to run.
HintPlace total inside braces
Begin the final line with print(f"Total: and include {total} before the
unit.
SolutionInsert the total in an f-string
print(f"Total: {total} {unit}")
Strings carry labels and units; numbers carry quantities that can be calculated. An f-string joins their displayed forms without confusing their roles. We can now name the parts of the Python lines that build this report.