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
Python

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.

1 / 9

Use Next and Back to run one line at a time. Keep your eye on which positions each line uses.

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

Watch out: A slice stops BEFORE its stop index. word[1:4] uses positions 1, 2 and 3, never position 4.

What do you think?

One step past the end

Same string as before: word = 'Python'. This time the program runs print(word[6]).

What happens? Pick the idea closest to what you think right now.
How sure are you?

Your turn to build

Build a new string, one character at a time

Complete the program so it builds a new string in which every letter of word appears twice. 'hop' should become 'hhoopp'.

  1. word = 'hop'One new tool first: with strings, + joins them end to end to make a new string. That's called concatenation, and it's how you build a string up.
  2. missing step
Which line is step 2?

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.

10 of 10 still to sort.

Programs do five everyday jobs with text. Each line of code below does exactly one of them.

Watch out: A slice and a search both involve part of a string, but they do different jobs. A slice takes a piece OUT of the string; in asks whether a piece is IN it.

ord() and chr(): mirror images

ord()vschr()

Every character also has a number behind it. Memory trick: chr() hands you back a CHaRacter.

Focus

Direction

ord()

Character → number

chr()

Number → character

The insight

This is the whole difference between them, and it's exactly the part people mix up. Decide which way you're going first.

What goes in the brackets

ord()

One character, e.g. 'a'

chr()

A whole number, e.g. 97

What comes back

ord()

Its ASCII value: ord('a') is 97

chr()

The character with that value: chr(97) is 'a'

Capital A

ord()

ord('A') is 65

chr()

chr(65) is 'A'

Put it all together

Write it: a username generator

Tools you'll need: a slice, word[start:stop]; + to join strings; and random.choice(text), which picks one random character from text, as long as the program has run import random first.

first_name = 'Priya' and surname = 'Shah'. Write Python code that creates a username made of the first three letters of first_name, then the first three letters of surname, then one random character chosen from the string '0123456789'. Display the username. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1A string is a collection of characters inside quote marks: letters, spaces, punctuation, digits and any other symbols. Digits in a string are handled as text characters, not numbers.
2Each character has an index, its position in the string. Indexing starts at 0, so for word = 'Python', word[0] is 'P' and word[3] is 'h'.
3len() returns the number of characters, including spaces and punctuation. The last character is at len(word) - 1, or word[-1]. An index greater than len(word) - 1 causes an index out of range error.
4A slice word[start:stop] includes the start index and stops before the stop index, so for word = 'HELLO', word[1:4] is 'ELL'. A substring is just another string, so == and in work on it too.
5+ joins strings (concatenation) and adds no space. To build a string in a loop, set it to '' before the loop, then concatenate onto its end inside the loop.
6for character in word visits each character in turn. for i in range(len(word)) visits each index, and word[i] gives the character at that position.
7upper() and lower() convert all the letters in a string to uppercase or lowercase. in checks whether a substring is contained in a string.
8ord() converts a character into its ASCII value and chr() does the reverse: ord('a') is 97 and chr(97) is 'a'. After import random, random.choice(word) picks a random character from word.

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?
A collection of characters inside quote marks: letters, spaces, punctuation, digits or any other symbols. Digits in a string are text, not numbers.
What does len() return?
The number of characters in a string, counting spaces and punctuation. len('Computer Science!') is 17.
What is an index?
A character's numerical position in a string. Python starts counting at 0, so the first character is word[0].
Where is the last character of a string?
At index len(word) - 1. Python also lets you use word[-1].
When do you get an index out of range error?
When the index you give is greater than the length of the string minus one.
What does word[start:stop] give you?
A substring (a slice): from 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, so cherry + drops gives cherrydrops.
How do you build up a new string with a loop?
Set a variable to a blank string, '', before the loop. Inside the loop, concatenate the new characters onto its end.
What are the two ways to loop over a string?
for character in word gives you each character in turn. for i in range(len(word)) gives you each index, and word[i] is the character there.
What do upper() and lower() do?
upper() converts all the letters in a string to uppercase; lower() converts them all to lowercase.
What does in do with two strings?
It checks whether the first one (a substring) is contained in the second, e.g. '@' in email.
What are substrings used for?
Jobs like validation (does an email address contain the right parts?), creating usernames and identifying file extensions.
What do ord() and chr() do?
ord() converts a character to its ASCII value; chr() converts a number back into a character. ASCII gives every character a unique number, with A at 65.
How do you pick a random character from a string?
import random, then random.choice(my_string). Forget the import and you get NameError: name 'random' is not defined.

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