Community resourceWorksheet
1CP2-CT-2.2 Substrings and string methods
Part 2 of 8 · 1CP2-CT-2 · Selection, strings and while loops
The substring worksheet, adding slicing and the common string methods to indexing.
Students will:
- slice from a start position up to, but not including, a stop position
- explain why a method returns a new string rather than changing the original
- build a short identifier from parts of an input
- use a position search and handle the not-found result
- combine methods to clean and reformat a value
Inside: 7 explanation cells, 2 multiple-choice questions, 3 Python tasks, 2 fill-in-the-blanks cells and 1 written answer. 19 marks, about 45 minutes.
Series: 1CP2-CT-2 · Selection, strings and while loops, part 2 of 8.
Shared by Coding PathwayVerified teacher
- 15 cells
- About 45 minutes
- CC BY-SA 4.0
- Shared 17 Aug 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Substrings and string methods
A substring is a continuous section of a string. Python can also return changed copies of strings using methods such as .upper(), .lower(), .replace() and .strip(" ").
Strings are immutable: these operations return a new string. They do not silently alter the original variable.
1. Slice from start up to stop
The pattern text[start:stop] includes start but excludes stop.
For code = "CS2026":
code[0:2]givesCS;code[2:6]gives2026;code[:2]also givesCS;code[2:]gives everything from index 2 onward.
The excluded stop position prevents adjacent slices from overlapping.
animal = tiger, animal[1:4] gives gap 1, animal[:2] gives gap 2, and animal[2:] gives gap 3.- er
- ig
- ige
- ti
- ger
- tiger
Why does word[1:4] contain three characters?
- AIndex 1 is skipped.
- BIndex 4 is included twice.
- CThe stop position 4 is excluded.
- DPython begins slices at index 1.
2. Methods return useful results
| Expression | Result | Purpose |
|---|---|---|
name.upper() | uppercase copy | normalise case |
name.lower() | lowercase copy | normalise case |
name.strip(" ") | copy without surrounding spaces | clean input |
name.replace("-", " " ) | copy with replacements | substitute text |
name.find("x") | first position, or -1 | search safely |
name.isalpha() | Boolean | test whether all characters are letters |
A method call uses a dot after the string. Save the returned value if it will be needed later.
raw = " Ada-Lovelace "
clean = raw.strip(" ")
display = clean.replace("-", " ").upper()
print(display)Explain why raw still contains its surrounding spaces after this program has run, while display does not.
Use the idea that string methods return new strings.
Students type their answer here.
3. Build a short identifier
For a supplied first_name and year, create identifier from the first three lowercase letters of the name followed by the year. With Charlotte and 26, the result is cha26.
first_name = "Charlotte"
year = "26"
# Create and output identifier.
4. Position and not-found behaviour
find() returns the first matching position. If the text is absent, it returns -1; this lets a program make a decision later. By contrast, index() raises an exception when no match exists.
Example: "banana".find("na") is 2 because the first na begins at index 2.
planet.find("net") returns the gap 1 where the substring begins. If it is absent, find() returns gap 2. The method that instead raises an exception when absent is gap 3.- -1
- 0
- first position
- index()
- length
5. Independent manipulation
Input a phrase. Store a cleaned lowercase copy in cleaned, store its first four characters in prefix, then output only prefix.
It will be tested with COMPUTING and Data . Both contain at least four non-space characters.
# Input phrase, clean it, select the prefix, then output it.
Which expression produces a lowercase copy of title without surrounding spaces?
- Atitle.lower.strip
- Btitle.strip(" ").lower()
- Clower(strip(title))
- Dtitle = strip.lower()
Route forward
You can now manipulate strings within the Pearson PLS scope. Later series revisit formatting, validation and more substantial string processing. Next, decisions allow programs to respond differently to data.