Now you make strings do work — change their case, search inside them, and format clean output without the clumsy str() from Tuesday’s lab.
Same chapter as yesterday, the second half: methods and f-strings. Enrolled in our Runestone course? Open it from there so your progress counts.
Still in Programs. In 2.6 a string had a length and an order. Today it gets behavior — the small built-in tools that turn raw text into the tidy output real programs produce.
By the end of class you’ll have written the username generator yourself. It’s the first program that feels like something a real system would actually do.
A string method is an action attached to a string with a dot. The string stays the same; the method hands back a new string. Two you’ll use constantly:
| Method | Does | Example |
|---|---|---|
| .upper() | all capitals | "hi".upper() → "HI" |
| .lower() | all lowercase | "Hi".lower() → "hi" |
Search inside a string with in: it asks “is this
substring somewhere in there?” and answers True or
False. "cat" in "concatenate" is True. (That
True/False answer is your bridge to next week’s decisions.)
Format output with an f-string — put an f
before the quotes and drop variables straight into { }. No more
+ str(...) gymnastics from the 2.5 lab:
each = 12.5 print("Each owes $" + str(each))
each = 12.5 print(f"Each owes ${each}")
Hover or tap a line to light its twin. Same output, far less clutter — the
{each} is replaced by the variable’s value automatically, no str()
needed.
.upper() —
it just uses DISPLAY with values separated by commas. These methods are Python
conveniences you write; the exam tests the idea (case, searching, building
output), not this exact syntax. Learn them because they make your projects clean and your Create
PT readable.
Type a first and last name and watch every string tool fire at once — the same pipeline a school system runs on every new student.
.upper() and .lower() change case.in tests whether one string
is a substring of another, giving True or False.Also today (Python conveniences): the f-string —
f"..." with {variables} inside — for building clean output.
Five questions. Pick an answer for instant feedback.
A pipeline like the one you just built assumes names are simple: one first, one last, plain letters. Real names aren’t. People have hyphens (Nguyen-Smith), apostrophes (O’Brien), accents (Muñoz), a single name, or four names. Systems that were coded for the “typical” case quietly mangle everyone else — a student named O’Brien gets an account that breaks, or two different people collide onto the same username.
This is the same shape you named back in Unit 1: a reasonable technical decision, made for the common case, pushes its costs onto whoever doesn’t fit the assumption. Good programmers ask “whose name breaks this?” before shipping. It’s a habit, and it’s worth points on the AP written responses too.
+ str(...) your
calculator lab needed for clean output.in returns True/False, and True/False is the
raw material of every if statement in Week 3.