🔤 Working with Text, lesson 3 of 5
String methods
Change case, trim spaces, replace, count and search inside text.
10 min
2 exercises
2 quizzes
0/4 solved
Getting Python ready… examples can run in a moment.
Strings come with built-in functions called
methods. You call a method with a dot after the
string: text.upper().
Methods never change the original string (strings are immutable). They return a new one, so store or print the result.
Changing case and trimming
strip() removes spaces at both ends only. The last line shows text itself is unchanged.
Searching and replacing
find returns the index where the text starts (or
-1 if it's missing). in answers with True or
False.
What does this print?
Because each method returns a string, you can
chain them: text.strip().lower() first strips,
then lowercases the result. That's great for cleaning
up what users type.
Tidy the username
Users type messy things. Clean raw so that
username has no spaces at either end and is all
lowercase: ada_l.
What does this print?
Polite robot
Make the robot polite: replace every "do" in
order with "please do" and store the result in
polite.