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

my_string=Pythonindex numbers=P0 y1 t2 h3 o4 n5

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.

1 / 10

Step through the trace — every value is the lesson’s, not run by the page.

Step 1 of 10: 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..

Computer Science · Strings

One of these lines will crash

This program should print the first and last letters of my_string joined together. One line is wrong. Which one?

A student's program — which line goes wrong?

Computer Science · Strings

What does this line print?

A program contains this line: print("5" + "3")

Which idea is closest to what you think?
How sure are you?

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

6 of 6 still to sort.

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.

1import random
2my_string = ""
3for letter in "asp":
4 new_string = random.choice("pineapple")
5 my_string = my_string + new_string
6print(my_string)
letter
new_string
my_string
Output
Press Start to run the first line.
·

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

1A string is a collection of characters in quote marks. Digits inside quote marks are text characters, not numbers.
2Indexing starts from 0. The last index is len(my_string) - 1, and a larger index causes an index out of range error.
3word[start:stop] includes the start index but not the stop index.
4Concatenation joins strings with +. Upper() and lower() change case. == checks equality and in checks whether one string is contained in another.
5chr() converts a number to a character and ord() converts a character to its ASCII value. random.choice() needs import random first.

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?
A collection of characters (letters, spaces, punctuation, digits and other symbols), written inside quote marks in Python.
What happens to digits inside quote marks?
They are handled as text characters, not numbers.
What does len() return?
The number of characters in the string, including spaces and punctuation.
Which index does the first character have?
Index 0. Indexing starts from 0.
Where is the last character of my_string?
At index len(my_string) - 1 (Python also allows -1).
What causes an index out of range error?
Using an index greater than the length minus one.
What does word[start:stop] include?
The character at the start index, up to but not including the stop index.
What is concatenation?
Joining strings together with + to make a new string. No space is added.
What do upper() and lower() do?
Convert all the letters in a string to uppercase or lowercase.
What do == and in check?
== checks whether two strings are equal. in checks whether one string is contained in another.
What is ASCII?
A character set that gives each character a unique numerical value. The codes increase by one through the alphabet.
What do chr() and ord() do?
chr() converts a number to its character. ord() converts a character to its ASCII value.
How do you pick a random character from a string?
Write import random at the start, then use random.choice(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 posted

More Edexcel GCSE Computer Science topics

See the full Edexcel Computer Science curriculum →

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.