How it works
Strings are immutable sequences. Formatted string literals interpolate expressions directly and are the clearest way to build output.
Build messages with formatted literals rather than repeated concatenation.
Common error
Expecting a string method to modify in place. Every one returns a new string.
Worked example
Python
name = "Ada"
score = 7 / 2
print(f"{name}: {score:.2f}")Output
Ada: 3.50An f-string inserts expressions into text. The .2f format shows two digits after the decimal point; it changes the display rather than the stored numeric value.
Exercises
1.3.1 · Trace
Determine the exact output and explain the state changes.
Python
word = "Python"
print(word[1:4])
print(word[-1])Show solution
Output
yth
nThe slice includes indices 1 through 3; index -1 is the last character.
1.3.2 · Write code
Format the price 12.5 as Price: $12.50.
Show solution
Use two decimal places in an f-string.
One valid solution
price = 12.5
print(f"Price: ${price:.2f}")Expected output
Price: $12.50