← IGCSE Computer Science Paper 2

SUBSTRING: extracting part of a string

Returns part of a string: length characters taken from the start position. SUBSTRING("Happy Days", 1, 5) gives "Happy". The result is always a string.

Understanding SUBSTRING: extracting part of a string in IGCSE Computer Science Paper 2

What it does: The start is 2 and the length is 4, so the call extracts four characters from position 2 and the result is assigned to Part with ←. Start 1 and length 1 give just the first character, and OUTPUT displays the string that SUBSTRING returns. Start and length must be positive whole numbers (integers). 2.5 is a REAL, so it is not valid. The variable should be declared as STRING (or CHAR for one character). The assigned value must match the variable's data type.

Common mistakes: The start and length are swapped. This takes 1 character from position 5, which gives "y". To get "Happy" it must start at 1 and take 5 characters: SUBSTRING(Word, 1, 5). Positions in Cambridge pseudocode start at 1, so a start of 0 is not valid. The first character is taken with SUBSTRING(Name, 1, 1).

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 337–346) and word search (Modules 337–357).

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?
"Days""Happy"
Matches
SUBSTRING("Happy Days", 5)
↓
The length argument is missing. SUBSTRING needs three arguments: the string, the start and the length.
Applying
Meanings
Term
SUBSTRING(<string>, <start>, <length>)
Meaning
Returns part of a string: length characters taken from the start position. SUBSTRING("Happy Days", 1, 5) gives "Happy". The result is always a string.
Practice
DECLARE Word : STRING
Testing
Gaps
Part ← ("Happy Days", 1, 5)
Check Answer
Spots
3Word ← "Happy Days"
4Part ← SUBSTRING(Word, 5, 1)

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

The 'Elephant' badge — OOP Heavyweight, 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. "Days"
Start at position 1 and take 5 characters: H, a, p, p, y. That gives "Happy".
2. In SUBSTRING("Computer", 1, 3), what does the last number (3) mean?
A. How many characters to take ✓
B. The position where the extract stops
The third argument is the length: the number of characters returned. It is not an end position.
3. Which call returns "i" from the string "Science"?
A. SUBSTRING("Science", 1, 3)
B. SUBSTRING("Science", 3, 1) ✓
Start 3, length 1 takes just the character at position 3, which is "i". Start 1, length 3 gives "Sci".
4. What does SUBSTRING("Pizza", 2, 1) return?
A. "iz"
B. "i" ✓
A length of 1 takes one character only, the one at the start position (position 2 is "i").
5. What is the data type of the result of SUBSTRING("Pizza", 2, 1)?
A. STRING, even though it is one character ✓
B. CHAR, because it is one character
SUBSTRING always returns a string, whatever the length. The result is a STRING.

Match the Pairs

SUBSTRING("Happy Days", 1, 5) → Returns "Happy": 5 characters taken from position 1
SUBSTRING(Word, 3, 1) → Returns one character: the character at position 3 of Word, as a string
Take the 4 characters of Name that begin at position 2 and store them in Part → Part ← SUBSTRING(Name, 2, 4)
Output the first character of Name by extracting one character from the start → OUTPUT SUBSTRING(Name, 1, 1)
SUBSTRING("Happy Days", 5) → The length argument is missing. SUBSTRING needs three arguments: the string, the start and the length.
Letter: INTEGER Letter ← SUBSTRING(Word, 1, 1) → SUBSTRING always returns a string, so it cannot be assigned to an INTEGER variable.

Key Term

SUBSTRING(<string>, <start>, <length>)
Returns part of a string: length characters taken from the start position. SUBSTRING("Happy Days", 1, 5) gives "Happy". The result is always a string.

Fill in the Blanks

1. Part ← SUBSTRING("Happy Days", 1, 5)
Hint: Type the function name that extracts part of a string.
2. OUTPUT SUBSTRING("Happy Days", 7, 4)
Hint: Type the start position of the first character wanted. Positions start at 1.
3. Name ← "Happy Days" Part ← SUBSTRING(Name, 1, 5)
Hint: Type the length: how many characters to take from the start position.
4. Letter ← SUBSTRING("Happy Days", 7, 1)
Hint: Type the length needed to take exactly one character.
5. DECLARE Part : STRING Part ← SUBSTRING("Happy Days", 1, 5)
Hint: Type the data type of the variable that stores the SUBSTRING result.

Spot the Error

Question 1
A DECLARE Word : STRING
B DECLARE Part : STRING
C Word ← "Happy Days"
D Part ← SUBSTRING(Word, 5, 1)
E OUTPUT Part
Line D — The start and length are swapped. This takes 1 character from position 5, which gives "y". To get "Happy" it must start at 1 and take 5 characters: SUBSTRING(Word, 1, 5).
Question 2
A DECLARE Letter : STRING
B Name ← "Cambridge"
C Letter ← SUBSTRING(Name, 0, 1)
D OUTPUT Letter
Line C — Positions in Cambridge pseudocode start at 1, so a start of 0 is not valid. The first character is taken with SUBSTRING(Name, 1, 1).
Question 3
A Text ← "IGCSE 0478"
B Code ← SUBSTRING[Text, 7, 4]
C OUTPUT Code
Line B — SUBSTRING is a function, so its arguments go inside round brackets ( ), not square brackets. It should be SUBSTRING(Text, 7, 4).
Question 4
A Word ← "Computer"
B Part ← SUBSTRING(Word, 1, -3)
C OUTPUT Part
Line B — The length must be a positive integer because you cannot take a negative number of characters. It should be SUBSTRING(Word, 1, 3), which gives "Com".
Question 5
A DECLARE Word : STRING
B DECLARE Part : INTEGER
C Word ← "Happy Days"
D Part ← SUBSTRING(Word, 7, 4)
E OUTPUT Part
Line B — SUBSTRING always returns a string, so the variable that stores the result must be declared as STRING, not INTEGER.

Student with a class code? Join your class →