GCSE · Computer Science · Edexcel · Spec 1CP2
String manipulation
Here is a trick that makes string questions easy. Before you answer anything, write numbers under the letters. Start at 0.
Computer Science · Strings
Number the letters first
Step through each line. Say the answer out loud before you click, then check it.
Before each click, work out what this line prints. Write the index numbers under the letters if it helps.
1 my_string = "Python"2 print(len(my_string))3 print(my_string[0])4 print(my_string[3])5 print(my_string[len(my_string) - 1])6 word = "HELLO"7 print(word[0:2])8 print(word[1:4])9 word = "pineapple"10 print(word[4:9])
Variables
Output
Step 1: Six characters, and the numbers under them run 0 to 5, not 1 to 6. Keep those numbers in your head for the next few lines.
Step through the trace — every value is the lesson’s, not run by the page.
Computer Science · Strings
What is this expression doing?
Each expression does one string job. Sort them by the job.
Still to sort
Finding the length (0)
How many characters are there?
Joining strings (0)
Sticking strings together to make a new one.
Extracting a substring (0)
Taking part of a string out.
Converting case (0)
Turning every letter to upper or lower case.
Searching or checking (0)
Is one string inside another, or are two strings equal? Done with in and ==.
Computer Science · Algorithms
Trace table — dry run the code
Build a password one random character at a time. The three random picks shown are ONE possible run. Run it again and you would usually get different letters.
Ready when you are — step through one line at a time.
Predict, then check
You are told ord("a") is 97 and that B is 66.
What does chr(98) give?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Every character has a number. Learn to see it and indexing, slicing and joining all fall into place.
What you need to know
- A string is text in quote marks. Spaces, punctuation and digits are all characters, and digits are text, not numbers.
- len() counts every character, spaces and punctuation included. len("Python") is 6.
- An index is a character's position, written in square brackets after the string. Counting starts at 0.
Have a goYour mate says the P in "Python" is at index 1, because it's the first letter. Are they right? Where is it?
No. The P is at index 0.
Counting starts at 0, so the first character sits at index 0 and not at index 1.
- The last character is at index len(my_string) - 1 (-1 works too). A larger index causes an index out of range error.
- A slice word[start:stop] includes the start index but stops just before the stop index.
Have a goWrite the index numbers under the letters of HELLO. Then what does word[2:4] give?
LL
The slice includes index 2 and stops just before index 4, so it takes indexes 2 and 3, which are both L.
- Concatenation joins strings with +, so "cherry" + "drops" gives cherrydrops, with no space.
- upper() and lower() convert all the letters in a string to uppercase or lowercase.
- == checks whether two strings are equal, and in checks whether one string is contained in another.
Have a goDoes "ps" in "asp" give True or False? What about "sp" in "asp"?
"ps" in "asp" is False. "sp" in "asp" is True.
in checks whether the whole string is contained, in that order. asp contains sp, but it does not contain ps.
- ASCII gives each character a unique number and the codes go up by one through the alphabet. chr() and ord() convert between them.
- random.choice(string) picks a random character from a string, after import random at the start of the program.
The big picture
A string is a row of characters, and each one has an index starting at 0. Once you can see the numbers you can find the length, pick out one character, slice out a substring, join strings with +, change case, check with == and in, and convert characters with chr() and ord().
Key points
Worked example
Problem
word = "onion". Write one line that checks whether the first two letters of word are the same as the last two letters, and say what it gives.
⚠ Watch out
Thinking word[1:4] includes the character at index 4, or that the last character is at index len(word). The start is included, the stop is not, and the last index is len(word) - 1.
Memory hook
Number the letters from 0. A slice starts ON its first number and stops just BEFORE its last: pineapple[4:9] is apple.
Check yourself
Try it cold. word = "robot". Number the letters from 0, then work out word[1:3] and len(word). Answer: ob, and 5.
Flashcards
(13)What is a string?
What happens to digits inside quote marks?
What does len() return?
Which index does the first character have?
Where is the last character of my_string?
What causes an index out of range error?
What does word[start:stop] include?
What is concatenation?
What do upper() and lower() do?
What do == and in check?
What is ASCII?
What do chr() and ord() 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
- Programming – sequencing
- Programming arithmetic operators
- Programming logical operators
- Programming relational operators
- Reading, writing, analysing and refining programs
- Single entry/exit code blocks
- Structural components of programs
- Structured data types
- User input handling
- Validation – length check
- Validation – presence check
- Validation – range check
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 21 November 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.