Strings and f-strings
Worked examples
Read the claim, then run it and check the machine agrees. One at a time — nothing here is taken on trust.
print(" hi ".strip()) # trim the outer spaces
print("sofr".upper()) # shout it
print("a,b,c".split(",")) # one string -> a list
print("USD/EUR".replace("/", "-")) # swap every slash for a dash
print("EURUSD".startswith("EUR")) # a yes/no question
print("-".join(["2026", "07", "12"])) # a list -> one joined string
Six calls, six results: 'hi', 'SOFR', ['a', 'b', 'c'], 'USD-EUR',
True, and '2026-07-12'. Each method returns a new value and leaves
the original text untouched. split turns a string into a list; join
does the reverse, gluing a list back together with a chosen separator.
print(f"{1234.5:,.2f}") # money: thousands + 2 decimals
print(f"{0.0525:.2%}") # a rate as a percent
print(f"{0.0525 * 1e4:.1f} bp") # basis points, 1 decimal
print(f"{'sofr':>8}") # right-align in a width-8 field
print(f"{2 + 2}") # any expression runs inside {}
The results are '1,234.50', '5.25%', '525.0 bp', ' sofr', and
'4'. The part after : is the format spec — , groups thousands, .2f
fixes decimals, .2% scales by 100 and appends %, and >8 right-aligns
in a fixed width. Everything before the : is just a value or expression.
t = "AAPL"
print(t[0]) # first character
print(t[-1]) # last character
print(t[1:3]) # a slice: positions 1 and 2
print(f"{2.5:.0f}") # halfway -> rounds to even
print(f"{3.5:.0f}") # halfway -> rounds to even
Indexing gives 'A', 'L', and the slice 'AP' (position 3 is
excluded). The last two lines show the surprise: f"{2.5:.0f}" is '2'
but f"{3.5:.0f}" is '4'. Rounding a halfway value goes to the nearest
even digit, so 2.5 rounds down and 3.5 rounds up.
Check the concept
One question at a time. Unsure? Revisit the lecture, then answer.
The challenge
Pass the quiz to unlock the challenge — your code will still be waiting here.