Learning objectives
By the end of this section you should be able to
- Use f-strings to simplify output with multiple values.
- Format numbers with leading zeros and fixed precision.
F-strings
A formatted string literal (or f-string) is a string literal that is prefixed with "f"
or "F"
. A replacement field is an expression in curly braces ({}
) inside an f-string. Ex: The string f"Good morning, {first} {last}!"
has two replacement fields: one for a first name, and one for a last name. F-strings provide a convenient way to combine multiple values into one string.
Concepts in Practice
Basic f-strings
Formatting numbers
Programs often need to display numbers in a specific format. Ex: When displaying the time, minutes are formatted as two-digit integers. If the hour is 9 and the minute is 5, then the time is "9:05" (not "9:5").
In an f-string, a replacement field may include a format specifier introduced by a colon. A format specifier defines how a value should be formatted for display. Ex: In the string f"{hour}:{minute:02d}"
, the format specifier for minute
is 02d
.
Format | Description | Example | Result |
---|---|---|---|
|
Decimal integer (default integer format). |
|
|
|
Decimal integer, with comma separators. |
|
|
|
Decimal integer, at least 10 characters wide. |
|
|
|
Decimal integer, at least 10 characters wide, with leading zeros. |
|
|
|
Fixed-point (default is 6 decimal places). |
|
|
|
Fixed-point, rounded to 4 decimal places. |
|
|
|
Fixed-point, rounded to 4 decimal places, at least 8 characters wide. |
|
|
|
Fixed-point, rounded to 4 decimal places, at least 8 characters wide, with leading zeros. |
|
|
Concepts in Practice
Formatting numbers
Try It
Mad lib (f-string)
A mad lib is a funny story that uses words provided by a user. The following mad lib is based on four words (user input in bold):
Enter a name: Buster Enter a noun: dog Enter an adjective: super Verb ending in -ing: swimming Buster, the super dog, likes to go swimming.
Most of the code for this mad lib is already written. Complete the code below by writing the f-string.
Try It
Wage calculator
You just landed a part-time job and would like to calculate how much money you will earn. Write a program that inputs the time you start working, the time you stop working, and your hourly pay rate (example input in bold):
Starting hour: 9 Starting minute: 30 Stopping hour: 11 Stopping minute: 0 Hourly rate: 15
Based on the user input, your program should calculate and display the following results:
Worked 9:30 to 11:00 Total hours: 1.5 Payment: $22.50
For this exercise, you need to write code that (1) calculates the total payment and (2) formats the three output lines. Use f-strings and format specifiers to display two-digit minutes, one decimal place for hours, and two decimal places for payment. The input code has been provided as a starting point.
Assume the use of a 24-hour clock. Ex: 16:15 is used instead of 4:15pm.