← IGCSE Computer Science Paper 2

LENGTH: number of characters in a string

Returns an INTEGER: the number of characters in the string, including spaces. LENGTH("Happy Days") returns 10 and LENGTH("") returns 0.

Understanding LENGTH: number of characters in a string in IGCSE Computer Science Paper 2

What it does: LENGTH returns an integer (9 here), so an INTEGER variable is the correct place to store it. Name holds 7 characters including the space, so LENGTH(Name) outputs 7. Spaces count as characters. Happy (5) + space (1) + Days (4) = 10. An empty string has no characters, so its length is 0. LENGTH needs its argument in brackets: LENGTH(Name). A space is a character, so it is counted. Hi (2) + space (1) + there (5) = 8. LENGTH("cat") is the integer 3, so adding 1 gives 4. It does not join text. The characters are A, a space and B, which makes 3.

Common mistakes: LENGTH returns a whole number, so the variable that stores the result must be declared as INTEGER, not STRING. The Cambridge pseudocode function is called LENGTH, not LEN. It should be Size ← LENGTH(Name).

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 LENGTH("Happy Days") return?
910
Matches
Size ← LENGTH(Happy Days)
↓
The string literal has no quotation marks. It should be LENGTH("Happy Days").
Applying
Meanings
Term
LENGTH(Text)
Meaning
Returns an INTEGER: the number of characters in the string, including spaces. LENGTH("Happy Days") returns 10 and LENGTH("") returns 0.
Practice
DECLARE Size : INTEGER
Testing
Gaps
Size ← ("Happy Days")
Check Answer
Spots
1DECLARE Word : STRING
2DECLARE Count : STRING

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

The 'Starfish' badge — Five-Point Star, 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 LENGTH("Happy Days") return?
A. 10 ✓
B. 9
Spaces count as characters. Happy (5) + space (1) + Days (4) = 10.
2. Which data type does LENGTH return?
A. STRING
B. INTEGER ✓
LENGTH returns a whole number, so the result is an INTEGER.
3. What does LENGTH("") return?
A. 0 ✓
B. 1
An empty string has no characters, so its length is 0.
4. Which line correctly stores the length of Name in a variable?
A. Size ← LENGTH(Name) ✓
B. Size ← LENGTH Name
LENGTH needs its argument in brackets: LENGTH(Name).
5. Does LENGTH("Hi there") count the space?
A. No, it returns 7
B. Yes, it returns 8 ✓
A space is a character, so it is counted. Hi (2) + space (1) + there (5) = 8.

Match the Pairs

LENGTH("Happy Days") → Returns 10, because the space between the words counts as a character
LENGTH("") → Returns 0, because an empty string has no characters
LENGTH gives a whole number, so its result can be stored in an INTEGER variable → DECLARE Size : INTEGER Size ← LENGTH("Cambridge")
LENGTH counts the characters of the string held in a variable → Name ← "Sam Lee" OUTPUT LENGTH(Name)
Size ← LENGTH(Happy Days) → The string literal has no quotation marks. It should be LENGTH("Happy Days").
OUTPUT LENGTH("Hi there") - 1 // student expects 7 because spaces do not count → Spaces do count as characters, so LENGTH("Hi there") is 8. Do not subtract 1 for the space.

Key Term

LENGTH(Text)
Returns an INTEGER: the number of characters in the string, including spaces. LENGTH("Happy Days") returns 10 and LENGTH("") returns 0.

Fill in the Blanks

1. Size ← LENGTH("Happy Days")
Hint: Type the function name that counts the characters in a string.
2. DECLARE Total : INTEGER Total ← LENGTH("Happy Days") OUTPUT Total
Hint: Type the data type that stores a whole number such as a character count.
3. Count ← LENGTH("") OUTPUT Count // Output: 0
Hint: Type the number of characters in an empty string.
4. Name ← "Ava Lee" Size ← LENGTH(Name) OUTPUT Size
Hint: Type the variable that holds the string being measured.
5. Size ← LENGTH("Happy Days") OUTPUT Size // Output: 10
Hint: Type the assignment operator that stores the result in the variable.

Spot the Error

Question 1
A DECLARE Word : STRING
B DECLARE Count : STRING
C Word ← "Happy Days"
D Count ← LENGTH(Word)
Line B — LENGTH returns a whole number, so the variable that stores the result must be declared as INTEGER, not STRING.
Question 2
A DECLARE Name : STRING
B DECLARE Size : INTEGER
C Name ← "Happy Days"
D Size ← LEN(Name)
Line D — The Cambridge pseudocode function is called LENGTH, not LEN. It should be Size ← LENGTH(Name).
Question 3
A Word ← "Hi there"
B Size ← LENGTH(Word)
C OUTPUT "Characters: " Size
Line C — Values in an OUTPUT statement must be separated by commas. It should be OUTPUT "Characters: ", Size.
Question 4
A Phrase ← "Happy Days"
B // spaces count too
C Size ← LENGTH(Phrase) - 1
D OUTPUT Size
Line C — Spaces count as characters, so LENGTH("Happy Days") is already 10. Subtracting 1 gives the wrong answer; the line should be Size ← LENGTH(Phrase).
Question 5
A Empty ← ""
B Size ← LENGTH("Empty")
C OUTPUT Size
Line B — The quotes turn Empty into a string literal of 5 characters. To measure the empty string stored in the variable, use LENGTH(Empty), which gives 0.

Student with a class code? Join your class →