Kodewick
Lesson 5 of 31Contents

String methods

Lesson 5 of 31 · Part 1 · Basics · about 15 minutes

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.

main.py
1name = "wick"2print(name.upper())3print(name)4loud = name.upper()5print(loud)
Output
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

main.py
1title = "the lamp at the end of the street"2print(title.upper())3print(title.title())4print(title.capitalize())5print("MiXeD CaSe".lower())
Output
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:

main.py
1typed = "   wick   "2print("[" + typed + "]")3print("[" + typed.strip() + "]")4print("[" + typed.lstrip() + "]")5print("[" + typed.rstrip() + "]")
Output
[   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.

Guess first

What does this print?

1answer = "  YES  "2print("[" + answer.strip().lower() + "]")

Finding and counting

main.py
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)
Output
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.

main.py
1note = "Mill Road is dark. Light Mill Road first."2print(note.replace("Mill Road", "Oak Lane"))3print(note)4print(note.replace(".", ""))
Output
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.

Guess first

What does this print?

1print("banana".replace("a", "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.

main.py
1line = "oil,wick,glass"2parts = line.split(",")3print(parts)4print("Wick lights the lamps".split())5print(" + ".join(parts))
Output
['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:

main.py
1name = "wick"2street = "  mill road "3print(f"{name.title()} lives on {street.strip().title()}")4print(f"{name.upper()} has {len(name)} letters")
Output
Wick lives on Mill Road
WICK has 4 letters

Methods at a glance

MethodDoesExampleResult
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.

Exercise 1

Tidy a name

Someone typed their name carelessly. Clean it up so it prints as below.

It should print
Ada Lovelace
exercise1.py
1raw = "   aDA lOVELACE  "2print(raw)
Output
Press Run or Ctrl+Enter
Exercise 2

Date format

Print the date with dashes instead of slashes.

It should print
2026-11-01
exercise2.py
1date = "2026/11/01"2print(date)
Output
Press Run or Ctrl+Enter
Exercise 3

Comma list

Turn the words, separated by spaces, into one line separated by a comma and a space.

It should print
red, green, blue
exercise3.py
1colors = "red green blue"2print(colors)
Output
Press Run or Ctrl+Enter

What you learned

  • A method is called with a dot: text.upper(). It returns a new string; the original stays the same.
  • upper, lower, title and capitalize change capitals; strip removes whitespace at the ends.
  • Methods can be chained: text.strip().lower().
  • find gives an index or -1, count counts, and in gives True or False.
  • replace(old, new) swaps text; replacing with "" deletes it.
  • split cuts a string into a list; "glue".join(parts) puts it back together.
  • Method calls work inside an f-string's { }.