← IGCSE Computer Science Paper 2

LENGTH function

Returns the number of characters in a string as an integer, counting spaces. LENGTH("Happy Days") returns 10. Used in length checks.

Understanding LENGTH function in IGCSE Computer Science Paper 2

What it does: LENGTH returns an integer, so the result can be assigned to a variable with ←. A length check uses LENGTH in a condition. Using <> 4 catches any PIN that is too short or too long. The space between the two words counts as a character: Happy (5) + space (1) + Days (4) = 10. LENGTH counts the characters in a string and gives back a whole number (INTEGER). Cambridge pseudocode writes the function name in capitals, followed by the string in brackets: LENGTH(Password). H, i, space, t, h, e, r, e = 8 characters. Spaces are counted. At least 8 means 8 or more, so use >=. Using <= would accept only passwords of 8 characters or fewer.

Common mistakes: The Cambridge pseudocode function is called LENGTH, not LEN. The line should be OUTPUT LENGTH(Word). LENGTH is a function, so its argument must be inside brackets. The line should be Size ← LENGTH(Word).

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 LENGTH("Happy Days") return?
109
Matches
Size ← LENGTH "Cat"
↓
The string is not inside brackets. LENGTH is a function, so its argument must be in brackets: LENGTH("Cat").
Applying
Meanings
Term
LENGTH(<string>)
Meaning
Returns the number of characters in a string as an integer, counting spaces. LENGTH("Happy Days") returns 10. Used in length checks.
Practice
Name ← "Happy Days"
Testing
Gaps
OUTPUT ("Happy Days")
Check Answer
Spots
2Word ← "Happy Days"
3OUTPUT LEN(Word)

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

The 'Snow Leopard' badge — Silent Debugger, 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. 9
B. 10 ✓
The space between the two words counts as a character: Happy (5) + space (1) + Days (4) = 10.
2. What does the LENGTH function return?
A. The number of characters in a string, as an integer ✓
B. The first character of a string, as a CHAR
LENGTH counts the characters in a string and gives back a whole number (INTEGER).
3. Which is the correct way to write the function call in Cambridge pseudocode?
A. Password.LENGTH
B. LENGTH(Password) ✓
Cambridge pseudocode writes the function name in capitals, followed by the string in brackets: LENGTH(Password).
4. What does LENGTH("Hi there") return?
A. 8 ✓
B. 7
H, i, space, t, h, e, r, e = 8 characters. Spaces are counted.
5. Which check would normally use the LENGTH function?
A. Length check ✓
B. Presence check
A length check uses LENGTH to see how many characters were entered, for example making sure a password has at least 8 characters.

Match the Pairs

LENGTH("Happy Days") → Returns 10, because the space between the two words counts as a character
LENGTH(Password) >= 8 → A length check that is true only when the password has at least 8 characters
Store the number of characters in a name in a variable → NameSize ← LENGTH(Name)
Reject a PIN that does not have exactly 4 characters → IF LENGTH(PIN) <> 4 THEN
Size ← LENGTH "Cat" → The string is not inside brackets. LENGTH is a function, so its argument must be in brackets: LENGTH("Cat").
// LENGTH("Hi there") returns 7 OUTPUT LENGTH("Hi there") → The comment is wrong because spaces count as characters. LENGTH("Hi there") returns 8, not 7.

Key Term

LENGTH(<string>)
Returns the number of characters in a string as an integer, counting spaces. LENGTH("Happy Days") returns 10. Used in length checks.

Fill in the Blanks

1. OUTPUT LENGTH("Happy Days")
Hint: Type the function name that returns the number of characters in a string.
2. DECLARE Name : STRING INPUT Name Size ← LENGTH(Name)
Hint: Type the variable whose characters are being counted.
3. IF LENGTH(Password) >= 8 THEN
Hint: Type the relational operator meaning 'at least' in a minimum length check.
4. OUTPUT LENGTH("Happy Days") // shows 10
Hint: Type the number of characters in the string, remembering that the space counts.
5. IF LENGTH(Code) <> 4 THEN OUTPUT "Wrong length" ENDIF
Hint: Type the keyword that closes the IF statement in this length check.

Spot the Error

Question 1
A DECLARE Word : STRING
B Word ← "Happy Days"
C OUTPUT LEN(Word)
Line C — The Cambridge pseudocode function is called LENGTH, not LEN. The line should be OUTPUT LENGTH(Word).
Question 2
A DECLARE Size : INTEGER
B Word ← "Happy Days"
C Size ← LENGTH Word
D OUTPUT Size
Line C — LENGTH is a function, so its argument must be inside brackets. The line should be Size ← LENGTH(Word).
Question 3
A INPUT Password
B Size ← LENGTH("Password")
C IF Size ≥ 8 THEN
D OUTPUT "Accepted"
E ENDIF
Line B — The quotes make LENGTH measure the word Password (8 characters) instead of what the user typed. Remove the quotes: LENGTH(Password).
Question 4
A DECLARE Count : STRING
B Count ← LENGTH("Happy Days")
C OUTPUT Count
Line A — LENGTH returns a whole number, so the variable that stores it must be INTEGER, not STRING.
Question 5
A INPUT Code
B Size ← LENGTH(Code)
C IF Size = 5 THEN
D OUTPUT "Wrong length"
E ENDIF
Line C — The error message would show when the length is correct. The length check should test for an invalid length: IF Size <> 5 THEN.

Student with a class code? Join your class →