String methods (strip, split, join…)
Strings come with methods like strip(), lower(), split() and join() that return a new string or list without changing the original.
Used in 122 lessons of the course, from 00.01.
A method is a function that lives on a value. You call it with a dot: text.lower(). The common ones are strip() (removes spaces at both ends), lower() and upper() (change case), replace(a, b), split(sep) (cuts into a list) and sep.join(items) (glues a list into a string).
Strings are immutable. That's why every method returns a new value and the original stays the same. If you want to keep the result, assign it to a name.
Since each method returns a string, you can chain them. In lesson 00.01 you see key = term.strip().lower(): first it trims spaces, then it lowercases. That way " Agent " and "agent" end up as the same key.
Notice join: you call it on the separator, not on the list. Read it as "join with this separator".
Reach for it when: you need to clean, normalize, split or join text, for example before comparing it or using it as a key.
Examples
Common mistake
name = " Ada " name.strip() print(repr(name))
Why: strip() returns a new string. You didn't keep it, so it's lost and name stays the same.
How to write it: Assign the result: name = name.strip().
Common mistake
words = ["a", "b"]
print(words.join(", "))Why: join is a str method, not a list method.
How to write it: Call it on the separator: ", ".join(words).
