GCSE · Computer Science · Edexcel · Spec 1CP2
Strings
To get the first letter of 'Python', you ask for letter number 0. That one odd idea, counting from zero, unlocks almost everything a program does with text.
Computer Science · Strings
Number the boxes, then read them off
Two short strings and nine lines of Python. Each time you press Next, one line runs and the table shows which positions it used. The column headings are the positions.
Before you start: what do you think word[3] gives for 'Python', t or h? Commit to one, then step through and see.
1 word = 'Python'2 print(len(word))3 print(word[0])4 print(word[3])5 print(word[len(word) - 1])6 print(word[-1])7 word = 'HELLO'8 print(word[0:2])9 print(word[1:4])
| [0] | [1] | [2] | [3] | [4] | [5] | result |
|---|---|---|---|---|---|---|
| P | y | t | h | o | n |
Output
Step 1: Here's the trick: before you do anything with a string, number its characters. 'Python' is six characters in a row, and the numbering starts at 0, not 1. P is at position 0 and n is at position 5.
Use Next and Back to run one line at a time. Keep your eye on which positions each line uses.
Which job is it doing?
Sort the string handling
Pick a line of code, then choose the job it does.
Still to sort
Length (0)
How many characters are in the string?
Joining (concatenation) (0)
Adding two or more strings together.
Substring (slicing) (0)
Extracting part of a string.
Where the line is: You get a piece of the string back, not a yes-or-no answer about it.
Case conversion (0)
Changing the text to uppercase or lowercase.
Searching (0)
Finding a letter or word inside a string.
Where the line is: You're asking whether a piece is in there, not cutting it out.
Programs do five everyday jobs with text. Each line of code below does exactly one of them.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
A string is any run of characters inside quote marks: letters, spaces, digits, punctuation, anything. Give each character a numbered position and nearly every string job becomes easy.
What you need to know
- What a string is, and why digits inside one are treated as text
- How len() and zero-based indexing work, including the last character and the index out of range error
- How to slice a substring, and why the stop index is left out
- How to loop over a string, and how to build a new string with concatenation
- The five common string handling jobs: length, joining, substring, case conversion and searching
- What ord() and chr() do, and how to pick a random character with random.choice()
The big picture
A string is a collection of characters inside quote marks, and digits in a string are text, not numbers. Each character has an index, starting at 0. len() counts the characters, the last one is at len() - 1 (or -1), and any index beyond that is out of range. A slice runs from its start index up to, but not including, its stop index. + joins strings, upper() and lower() convert case, in searches for a substring, and a for loop visits each character. ord() and chr() convert between characters and ASCII values, and random.choice() picks a random character.
Key points
Worked example
Problem
word = 'robot' for i in range(0, 5, 2): print(word[i]) What does this program display?
⚠ Watch out
Counting positions from 1. The first character is at index 0, so the last one is at len(word) - 1, not at len(word), which is out of range. The same slip makes people include the stop index in a slice, but word[1:4] stops before position 4.
Memory hook
Zero is where you start, length minus one is where you end, and a slice always stops one box short.
Check yourself
code = 'X7-Blue'. Without running it, work out len(code), code[-1] and code[1:3]. Then say what happens with code[7], and why.
Flashcards
(14)What is a string?
What does len() return?
What is an index?
Where is the last character of a string?
When do you get an index out of range error?
What does word[start:stop] give you?
What is concatenation?
How do you build up a new string with a loop?
What are the two ways to loop over a string?
What do upper() and lower() do?
What does in do with two strings?
What are substrings used for?
What do ord() and chr() do?
How do you pick a random character from a string?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore Edexcel GCSE Computer Science topics
How this lesson was checked. This Edexcel GCSE Computer Science (specification 1CP2)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 29 September 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.