← IGCSE Computer Science Paper 2

SUBSTRING function

SUBSTRING(string, start, length) returns length characters beginning at position start. Position 1 is the first character, so this returns "Happy".

Understanding SUBSTRING function in IGCSE Computer Science Paper 2

What it does: Start at position 1 (the first character) with a length of 3 to get 3 characters, then assign the result with ←. Cambridge pseudocode counts string positions from 1, so SUBSTRING(Word, 1, 1) gives the first character. The last value is the length. It says how many characters to return, not where to stop. A length of 1 returns a single character. A length of 0 would return nothing. SUBSTRING only returns a copy of part of the string. To keep the result you must assign it to a variable. "Days" starts at position 7 and is 4 characters long, so the correct call is SUBSTRING("Happy Days", 7, 4).

Common mistakes: In SUBSTRING the first character is at position 1, so a start of 0 is invalid. The line should be First ← SUBSTRING(Word, 1, 5), which gives "Happy". SUBSTRING needs three arguments: the string, the start and the length. The length is missing. It should be SUBSTRING("Cloud", 3, 1) to pick out the single character "o".

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 37–46) and word search (Modules 27–46).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What does SUBSTRING("Happy Days", 1, 5) return?
"Happy""Happy "
Matches
SUBSTRING("Happy Days", 0, 5)
↓
Position 0 does not exist because the first character is at position 1, so the start value must be 1
Applying
Meanings
Term
SUBSTRING("Happy Days", 1, 5)
Meaning
SUBSTRING(string, start, length) returns length characters beginning at position start. Position 1 is the first character, so this returns "Happy".
Practice
DECLARE Code : STRING
Testing
Gaps
("Happy Days", 1, 5)
Check Answer
Spots
1Word ← "Happy Days"
2First ← SUBSTRING(Word, 0, 5)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Manta Ray' badge — Graceful Coder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What does SUBSTRING("Happy Days", 1, 5) return?
A. "Happy" ✓
B. "Happy "
Start at position 1 and take 5 characters: H, a, p, p, y. The space is the 6th character, so it is not included.
2. In Cambridge pseudocode, which position is the first character of a string?
A. Position 1 ✓
B. Position 0
Cambridge pseudocode counts string positions from 1, so SUBSTRING(Word, 1, 1) gives the first character.
3. In SUBSTRING("Computer", 4, 3), what does the 3 mean?
A. The position to stop at
B. The number of characters to return ✓
The last value is the length. It says how many characters to return, not where to stop.
4. What does SUBSTRING("Computer", 4, 3) return?
A. "put" ✓
B. "mpu"
Position 4 is 'p' (C=1, o=2, m=3, p=4). Taking 3 characters gives p, u, t, so the result is "put".
5. Which line picks out only the first character of Code?
A. SUBSTRING(Code, 1, 0)
B. SUBSTRING(Code, 1, 1) ✓
A length of 1 returns a single character. A length of 0 would return nothing.

Match the Pairs

SUBSTRING("Happy Days", 1, 5) → Returns "Happy": 5 characters starting at position 1, the first character
SUBSTRING(Word, 3, 1) → Returns just the third character of the string stored in Word
Take the first 3 characters of a string called Code and store them in Prefix → Prefix ← SUBSTRING(Code, 1, 3)
A format check that tests whether the first character of ID is the letter A → IF SUBSTRING(ID, 1, 1) = "A" THEN
SUBSTRING("Happy Days", 0, 5) → Position 0 does not exist because the first character is at position 1, so the start value must be 1
SUBSTRING("Happy Days", 7, 10) → The length is the number of characters to take, not an end position. Starting at 7 with length 10 runs past the end of the string

Key Term

SUBSTRING("Happy Days", 1, 5)
SUBSTRING(string, start, length) returns length characters beginning at position start. Position 1 is the first character, so this returns "Happy".

Fill in the Blanks

1. SUBSTRING("Happy Days", 1, 5)
Hint: Type the function name that returns part of a string.
2. SUBSTRING("Happy Days", 1, 5)
Hint: Type the start position of the first character (the first position in a string).
3. Word ← SUBSTRING("Happy Days", 7, 4)
Hint: Type the length: how many characters make up the word Days.
4. Initial ← SUBSTRING(Name, 1, 1) OUTPUT Initial
Hint: Type the length needed to pick out just one character.
5. IF SUBSTRING(ID, 4, 1) <> "-" THEN OUTPUT "Invalid format" ENDIF
Hint: Type the relational operator meaning not equal, so the format check fails when the 4th character is not a hyphen.

Spot the Error

Question 1
A Word ← "Happy Days"
B First ← SUBSTRING(Word, 0, 5)
C OUTPUT First
Line B — In SUBSTRING the first character is at position 1, so a start of 0 is invalid. The line should be First ← SUBSTRING(Word, 1, 5), which gives "Happy".
Question 2
A Letter ← SUBSTRING("Cloud", 3)
B OUTPUT Letter
C // Outputs o
Line A — SUBSTRING needs three arguments: the string, the start and the length. The length is missing. It should be SUBSTRING("Cloud", 3, 1) to pick out the single character "o".
Question 3
A DECLARE Code : STRING
B Code ← SUBSTRING(3, 3, "AB123")
C OUTPUT Code
Line B — The arguments are in the wrong order. SUBSTRING takes the string first, then the start position, then the length: SUBSTRING("AB123", 3, 3), which returns "123".
Question 4
A INPUT Name
B Initial ← SUBSTRING(Name, 1, 1)
C OUTPUT "Initial"
Line C — Putting Initial in quotes outputs the word Initial instead of the value stored in the variable. It should be OUTPUT Initial, with no quotes.
Question 5
A Word ← "Computer"
B Total ← LENGTH(Word)
C FOR I ← 1 TO Total
D Ch ← SUBSTRING(Word, 1, I)
E OUTPUT Ch
F NEXT I
Line D — The start and length are swapped. To pick out one character at a time, the start must be I and the length must be 1: SUBSTRING(Word, I, 1).

Student with a class code? Join your class →