Day 5 · ~13m

String Formatting

f-strings, concatenation, and essential string methods.

🧑‍💻

Yesterday we used + to join strings together, and honestly? It was kind of painful. I had to remember all the quotes, the plus signs, the str() conversion for numbers... There has to be a better way. In my spreadsheets I just click "merge cells" and it's done.

👩‍🏫

There absolutely is a better way — f-strings. They're Python's modern approach to building strings. You put an f before the opening quote, then drop any variable or expression inside curly braces {}:

name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
# My name is Alice and I am 25 years old.

No str() conversions. No juggling + signs. Python handles it all. You can even put calculations right inside the braces:

price = 9.99
quantity = 3
print(f"Total: ${price * quantity}")
# Total: $29.97
🧑‍💻

Oh! So f-strings are basically like mail merge but for code? Like when I set up "Dear {FirstName}" in a template?

👩‍🏫

That's a perfect analogy. The f at the start tells Python "this string has blanks to fill in." The curly braces mark where the blanks are. Python fills them at runtime.

Let me show you why this matters. Compare these two approaches for building the same output:

# Concatenation — hard to read, easy to mess up
result = "Item: " + item + " | Qty: " + str(qty) + " | Price: $" + str(price)

# f-string — same result, much clearer
result = f"Item: {item} | Qty: {qty} | Price: ${price}"

With the f-string, you can see the shape of the final output at a glance.

🧑‍💻

OK I'm sold on f-strings. But what about modifying strings themselves? Like, at work I'm always cleaning up messy data — extra spaces, wrong capitalization. Does Python have anything for that?

👩‍🏫

Python strings come with dozens of built-in methods — functions attached to the string itself. Here are the ones you'll reach for most:

message = "  hello, world  "

message.upper()         # "  HELLO, WORLD  "
message.lower()         # "  hello, world  "
message.strip()         # "hello, world" — removes leading/trailing spaces
message.replace("world", "Python")  # "  hello, Python  "
message.title()         # "  Hello, World  "

Here's the key thing: methods don't change the original string. Strings in Python are immutable — once created, they can't be modified. Each method returns a new string. So you need to save the result:

cleaned = message.strip().title()
print(cleaned)  # "Hello, World"

See how we chained two methods? .strip() removes the spaces first, then .title() capitalizes each word. They run left to right, like an assembly line.

🧑‍💻

That chaining thing is neat. What about checking what's inside a string? Like, I get customer emails and I need to verify they have an "@" sign.

👩‍🏫

Python has you covered:

email = "alice@example.com"

email.startswith("alice")   # True
email.endswith(".com")      # True
"@" in email                # True — checks if a substring exists
len(email)                  # 17 — the length of the string

The in keyword reads like English: "Is @ in email?" It gives you back True or False. You'll use this constantly.

🧑‍💻

OK so f-strings for building strings, methods for cleaning them up, and in for checking what's inside. Let me try putting this all together.

👩‍🏫

Perfect. Your challenge combines all three — you'll take raw data and format it into a clean receipt line.

Practice your skills

Sign up to write and run code in this lesson.

Already have an account? Sign in