GCSE · Computer Science · Edexcel · Spec 1CP2
Strings
Quick: what's the first letter of 'Python'? P, obviously. Python calls it letter 0. Crack that one weird idea and most of what programs do with text clicks.
Computer Science · Strings
Number the boxes, then read them off
Two short strings, nine lines of Python. Each press of Next runs one line, and the table shows which positions it used. The column headings are the positions.
Before you press anything: what will word[3] give for 'Python', t or h? Pick one, then step through and find out.
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: First job with any string: number the boxes. 'Python' is six characters in a row, and the numbering starts at 0, not 1. So P sits at position 0 and n sits at position 5.
Press Next to run one line at a time, and Back to rewind. Watch which numbered boxes each line touches.
Which job is it doing?
Sort the string handling
Pick a line of code, then choose the job it's doing.
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 is doing one of them. Which?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Letters, spaces, digits, punctuation: if it's inside quote marks, it's a string. Number every character's position and nearly every string job gets easy.
What you need to know
- A string is characters inside quote marks: letters, spaces, punctuation, symbols, even digits, which count as text, not numbers.
- len() counts every character in a string, and spaces and punctuation count too.
Have a goYour mate Sam swears len('Hi, Sam!') is 5, because 'only letters count'. What's the real answer?
8
H, i, the comma, the space, S, a, m and ! are all characters, so Sam's 5 has left out the comma, the space and the !.
- Each character has an index, its numbered position, and Python starts counting at 0.
- The last character is at len(word) - 1, or word[-1]; any bigger index is an index out of range error.
- A slice word[start:stop] includes the start index and stops just before the stop index.
Have a goword = 'PIZZA'. Before you peek: what does word[1:3] give?
'IZ'
Positions 1 and 2 hold I and Z, and the slice stops before index 3, so the second Z stays behind. 'IZZ' wrongly includes the stop index.
- + joins strings end to end (concatenation), and it never slips a space in for you.
- for character in word visits each character in turn; for i in range(len(word)) visits each index instead.
- upper() and lower() convert every letter's case, and in checks whether one string sits inside another.
- ord() turns a character into its ASCII value and chr() turns it back; ASCII values climb in sequence through the alphabet.
Have a goord('a') is 97. No table allowed: what's ord('c')?
99
ASCII values go up in sequence through the alphabet, so b is 98 and c is 99. Answering 3 counts c's place in the alphabet, which isn't its ASCII value.
- After import random, random.choice(word) picks one random character from word.
The big picture
A string is a collection of characters inside quote marks, and any digits in it are text, not numbers. Every character has an index, counting from 0. len() counts the characters, the last one lives at len() - 1 (or -1), and any index past 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() change case, in searches for a substring, and a for loop visits each character. ord() and chr() swap between characters and ASCII values, and random.choice() picks a random character once random is imported.
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 is at len(word) - 1, and len(word) itself 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
Start at zero, finish at length minus one, 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 26 October 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.