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
Python

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.

1 / 9

Press Next to run one line at a time, and Back to rewind. Watch which numbered boxes each line touches.

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

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

What do you reckon?

One step past the end

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

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

Your turn to build

Build a new string, one character at a time

Finish the program so it builds a new string where every letter of word appears twice. 'hop' should come out as 'hhoopp'.

  1. word = 'hop'One new tool first. With strings, + joins them end to end to make a new string. That's concatenation, and it's how you build a string up bit by bit.
  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'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.

10 of 10 still to sort.

Programs do five everyday jobs with text. Each line of code below is doing one of them. Which?

Watch out: Slicing and searching both involve part of a string, but they're 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 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, and it's exactly the bit people mix up. Work out 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 makes a username from 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.

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

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.
2Every 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, spaces and punctuation included. 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 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?
A collection of characters inside quote marks: letters, spaces, punctuation, digits or any other symbols. Digits inside a string are text, not numbers.
What does len() return?
How many characters are in a string, spaces and punctuation included. len('Computer Science!') is 17.
What is an index?
A character's numbered 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 write word[-1].
When do you get an index out of range error?
When the index you ask for 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 gets 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 hands you each character in turn. for i in range(len(word)) hands 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 (checking an email address contains 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 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.