String methods
Strings come with a set of built-in tools for everyday jobs: tidying what someone typed, searching for a word, swapping one word for another, breaking a line into pieces. These tools are called methods.
What is a method?
A method is a function that belongs to a value. You call it with a dot after the value: name.upper(). The parentheses are needed, even when they are empty.
1name = "wick"2print(name.upper())3print(name)4loud = name.upper()5print(loud)
WICK wick WICK
Look at the second line of output: name is still lowercase. Strings cannot change (lesson 4), so a string method never changes the original. It returns a new string. To keep the result, store it in a variable.
Upper and lower case
1title = "the lamp at the end of the street"2print(title.upper())3print(title.title())4print(title.capitalize())5print("MiXeD CaSe".lower())
THE LAMP AT THE END OF THE STREET The Lamp At The End Of The Street The lamp at the end of the street mixed case
title() capitalizes every word; capitalize() only the first letter of the whole string. lower() is the usual way to compare text without caring about capitals: "YES", "Yes" and "yes" all become "yes".
Trimming spaces
People often type spaces by accident before or after their text. strip() removes all whitespace (spaces, tabs and newlines) from both ends; lstrip() only from the left, rstrip() only from the right. The square brackets in this example make the spaces visible:
1typed = " wick "2print("[" + typed + "]")3print("[" + typed.strip() + "]")4print("[" + typed.lstrip() + "]")5print("[" + typed.rstrip() + "]")
[ wick ] [wick] [wick ] [ wick]
Spaces in the middle are left alone: " Mill Road ".strip() is "Mill Road".
Chaining methods
Each method returns a new string, so you can call another method on the result straight away. Python works from left to right.
What does this print?
1answer = " YES "2print("[" + answer.strip().lower() + "]")
strip() gives "YES", then .lower() is called on that and gives "yes".
Finding and counting
1sentence = "the lamp by the lake"2print(sentence.find("lamp"))3print(sentence.find("moon"))4print(sentence.count("the"))5print("lake" in sentence)6print("Lake" in sentence)
4 -1 2 True False
find() returns the index where the text first starts, or -1 when it is not there at all. count() says how many times it appears. If you only need yes or no, in is the clearest: it gives True or False. All three are case-sensitive.
Replacing text
replace(old, new) swaps every old for new, and gives you the result as a new string.
1note = "Mill Road is dark. Light Mill Road first."2print(note.replace("Mill Road", "Oak Lane"))3print(note)4print(note.replace(".", ""))
Oak Lane is dark. Light Oak Lane first. Mill Road is dark. Light Mill Road first. Mill Road is dark Light Mill Road first
The original note is unchanged. Replacing with "" is a quick way to delete something.
What does this print?
1print("banana".replace("a", "o")) replace changes every match, not just the first, so all three as become o.
Splitting and joining
split() cuts a string into pieces and gives you a list of them: the square brackets in the output (more on lists in lesson 9). Give it a separator, or leave the parentheses empty to split on spaces. join() does the opposite: it glues the pieces together, with the string you call it on in between.
1line = "oil,wick,glass"2parts = line.split(",")3print(parts)4print("Wick lights the lamps".split())5print(" + ".join(parts))
['oil', 'wick', 'glass'] ['Wick', 'lights', 'the', 'lamps'] oil + wick + glass
Note the order in join: the glue comes first, " + ".join(parts), not parts.join(...).
Methods in f-strings
Anything that gives a value can go inside the { } of an f-string, method calls included:
1name = "wick"2street = " mill road "3print(f"{name.title()} lives on {street.strip().title()}")4print(f"{name.upper()} has {len(name)} letters")
Wick lives on Mill Road WICK has 4 letters
Methods at a glance
| Method | Does | Example | Result |
|---|---|---|---|
upper() | all capitals | "Hi".upper() | "HI" |
lower() | all small letters | "Hi".lower() | "hi" |
title() | capital at the start of each word | "mill road".title() | "Mill Road" |
strip() | removes whitespace at both ends | " hi ".strip() | "hi" |
find(x) | first index of x, or -1 | "lamp".find("m") | 2 |
count(x) | how many times x appears | "oo".count("o") | 2 |
replace(a, b) | swaps every a for b | "cat".replace("c", "h") | "hat" |
split(x) | cuts into a list at each x | "a,b".split(",") | ['a', 'b'] |
x.join(parts) | glues parts with x between | "-".join(["a", "b"]) | "a-b" |
Tip: None of these change the string you call them on. If the result seems to vanish, check that you stored or printed it.
Practice
Write the code, then press Check. A hint costs nothing; the answer is there when you need it.
Tidy a name
✓Someone typed their name carelessly. Clean it up so it prints as below.
Ada Lovelace
1raw = " aDA lOVELACE "2print(raw)
Date format
✓Print the date with dashes instead of slashes.
2026-11-01
1date = "2026/11/01"2print(date)
Comma list
✓Turn the words, separated by spaces, into one line separated by a comma and a space.
red, green, blue
1colors = "red green blue"2print(colors)
What you learned
- A method is called with a dot:
text.upper(). It returns a new string; the original stays the same. upper,lower,titleandcapitalizechange capitals;stripremoves whitespace at the ends.- Methods can be chained:
text.strip().lower(). findgives an index or-1,countcounts, andingivesTrueorFalse.replace(old, new)swaps text; replacing with""deletes it.splitcuts a string into a list;"glue".join(parts)puts it back together.- Method calls work inside an f-string's
{ }.