Unit 2 · Programming 1 · Lesson 2.7

Strings II: Methods and f-Strings

Now you make strings do work — change their case, search inside them, and format clean output without the clumsy str() from Tuesday’s lab.

Big Idea 3 — Algorithms & ProgrammingReal-world text

Before this lesson — read on Runestone

Same chapter as yesterday, the second half: methods and f-strings. Enrolled in our Runestone course? Open it from there so your progress counts.

1

Where We Are

People→ Bits→ Programs→ Internet→ Security→ Big Data→ Impact

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.

2

The Hook

ada.lovelace@school.edu Every school generates usernames the same way: take a name, lowercase it, glue on a suffix. That tiny factory — run millions of times — is four string operations you’ll learn in the next fifteen minutes.

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.

3

The Idea

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:

MethodDoesExample
.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:

The old way (2.5)

each = 12.5
print("Each owes $" + str(each))

The f-string way

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.

On the exam. AP Pseudocode doesn’t have f-strings or .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.
4

Try It — The Username Factory

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.

first = last =
initials
username
shout
contains a vowel?
5

Vocabulary

string method Programs
A built-in action called on a string with a dot, returning a new string or value. .upper() and .lower() change case.
substring Programs
A run of characters found inside a larger string. 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.

6

Check

Five questions. Pick an answer for instant feedback.

0 of 5 answered
7

Impact Check

When the username factory meets a real name

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.

8

Connections

Came from 2.6 — strings had length and order. Now they have built-in behavior you call with a dot.
Fixes 2.5 — f-strings replace the awkward + str(...) your calculator lab needed for clean output.
Next in 2.8 — things break. You learn to name the three kinds of error and read a traceback like a map instead of a wall of red.
Sets up next week — in returns True/False, and True/False is the raw material of every if statement in Week 3.
← 2.6 Strings I