Methods: functions that belong to a value
In lesson 1-1 you met functions: named instructions like print() and type() that you call with parentheses around their input. A method is a function that belongs to one specific type of value, and you call it through a value with a dot: value.method(). upper is not a free-standing instruction, it exists only on strings, so you reach it through the string it will act on: "hi".upper() gives "HI".
Methods matter because real text is messy: users type stray spaces and random capitalization, files arrive half-formatted. Cleaning text before comparing or storing it is one of the most common jobs in working code, and these are the methods that do it:
| Method | What it does |
|---|---|
s.upper() / s.lower() | new string in one case |
s.strip() | remove spaces/newlines from both ends |
s.replace(old, new) | swap every occurrence |
s.count(x) | how many times x appears |
s.startswith(x) / s.endswith(x) | True or False |
s.title() | Capitalize Each Word |
One rule above all: strings are immutable, meaning they can never be changed in place. No method changes the original string. Every method hands you a new string, and if you do not save it in a variable, it is gone.
Cleaning a string, then transforming it
strip removes the stray spaces first, and every method after that builds a new string from clean.
s = " Hack University " clean = s.strip() print(clean) print(clean.upper()) print(clean.lower()) print(clean.replace("University", "Everything")) print(clean.count("i"))
Output
Hack University
HACK UNIVERSITY
hack university
Hack Everything
2Every one of those calls read clean and produced something new without altering it, which is why the later lines still operate on the properly capitalized original. The final 2 counts the lowercase i in University twice, in Universi and ty, and the count is case-sensitive.
Why login forms normalize text
Here is the case that makes these methods matter. A user typed their email with stray spaces and inconsistent capitals, so a direct comparison against the stored address fails even though both refer to the same mailbox.
typed = " Ada.Lovelace@Example.COM " stored = "ada.lovelace@example.com" print(typed == stored) print(typed.strip().lower() == stored)
Output
False True
The first comparison is False because == on strings is exact, and leading spaces plus capital letters are genuine differences in the characters. Normalizing one side with strip() and lower() removes precisely those cosmetic differences, and the second comparison then succeeds. This pairing sits near every login form ever written.
This snippet prints ada, in lowercase, which surprises most people the first time.
name = "ada" name.upper() print(name)
The reason is immutability. name.upper() does build the string "ADA", but nothing captures the result, so it is discarded immediately and name still points at the original "ada". Methods on strings never modify the value they are called on. Keeping the result requires assigning it, as in name = name.upper().
Methods can be chained, with each one acting on the result of the previous. Here strip() removes the surrounding spaces and title() capitalizes each word, and only the final result is stored.
messy = " ada lovelace " name = messy.strip().title() print(name) print(name.count("a"))
Output
Ada Lovelace
2Reading messy.strip().title() left to right shows the pipeline: strip produces "ada lovelace", and title turns that into "Ada Lovelace". The count of 2 is worth pausing on, because Ada Lovelace appears to contain three a-shaped letters. Counting is case-sensitive, so the capital A at the front is not a match, leaving the a in Ada and the a in Lovelace.
The same chaining pattern tidies a filename, and the cleaned value then feeds three separate questions about it.
filename = " Report_Final.PDF " clean = filename.strip().lower() print(clean) print(clean.endswith(".pdf")) print(clean.replace("_", "-"))
Output
report_final.pdf True report-final.pdf
Lowercasing first is what makes the .pdf test work, since the original ended in .PDF and endswith compares exactly. That method returns a bool, so it can be printed directly as True. The final replace takes two arguments, the text to find and the text to substitute, and swaps every underscore for a dash while leaving clean itself untouched.