AQA GCSE CSPaper 1 · Unit 2Lesson 10

Paper 1 · Unit 2 · CS-L2-10

String Handling

60 minutes · AQA 8525 · Paper 1 — Programming

Spec & Goals 3 min

AQA Spec 3.2.8 · String handling operations

By the end of this lesson you can:

  1. Find the length of a string and extract a substring from it.
  2. Concatenate strings and convert their case to upper or lower.
  3. Convert between a character and its character code.

Warm-Up 5 min

Last lesson you read input and rolled dice. Today we slice, join and measure text.

Quick starter

The word 'ROBOT' is a string of characters.

How many characters does it contain, and what is its first character?

Reveal the answer

It has 5 characters. The first character is 'R'. Counting characters is what LEN does.

Key Concept — working with strings 14 min

A string is a sequence of characters, such as a name or a word.

The operations you must know

OperationPseudo-codeWhat it does
LengthLEN(s)The number of characters in s.
SubstringSUBSTRING(start, end, s)Extract the characters from start to end.
PositionPOSITION(s, char)The index of the first char in s.
Concatenationa + bJoin two strings into one.
Case conversionSTRING_TO_UPPER(s)Convert to upper (or lower) case.
Character → codeCHAR_TO_CODE(c)The ASCII/Unicode number for a character.
Code → characterCODE_TO_CHAR(n)The character for a code number.

Worked Example — building a username 12 min

Problem: from a first name and a surname, build a username = the first letter of the first name + the surname (in upper case), then report its length.

Use first ← 'Aisyah' and last ← 'Tan'. Expected username: 'ATAN'.

AQA pseudo-code

first ← 'Aisyah'
last ← 'Tan'
initial ← SUBSTRING(0, 0, first)
username ← STRING_TO_UPPER(initial + last)
OUTPUT username
OUTPUT LEN(username)

The same in Python

first = "Aisyah"
last = "Tan"
initial = first[0]
username = (initial + last).upper()
print(username)
print(len(username))

The output is ATAN then 4. To find the character code of the first letter you would use ord("A"), giving 65; chr(65) turns it back into "A".

Try It Yourself 12 min

🟢 Easy

Goal: Write a line of AQA pseudo-code that outputs the length of a string stored in word.

Hint: use LEN(word) inside an OUTPUT.

🟡 Medium

Goal: Write pseudo-code that joins town and state with a comma and a space between them, e.g. 'Ipoh, Perak'.

Hint: concatenate three parts: town, the text ', ', then state.

🔴 Stretch

Goal: Write pseudo-code that inputs a full name, then outputs it in upper case together with its length.

📝 Exam Practice 10 min

Answer the way the examiner expects — the command word and the marks tell you how much to write.

State[1 mark]

State the output of LEN("ROBOT").

Mark scheme
  • 5 (1).
Write[2 marks]

Write an expression that joins the strings first and last into one string, with a single space between them.

Mark scheme
  • Concatenation of first and last using + (1).
  • A space ' ' joined in the middle, e.g. first + ' ' + last (1).
State[1 mark]

The string name ← 'ROBOT'. State the value of POSITION(name, 'B').

Mark scheme
  • 2 (1) — positions count from 0, so R=0, O=1, B=2.
Give[1 mark]

The character code of 'A' is 65. Give the character with code 66.

Mark scheme
  • 'B' (1) — the next code is the next letter.

Recap & Key Terms 3 min

A string is a sequence of characters. We measure it with LEN, cut it with SUBSTRING, find a character with POSITION, join strings by concatenation, change case, and convert between a character and its character code. Positions count from 0.

String
A sequence of characters, e.g. 'Aisyah'.
Length
The number of characters in a string, found with LEN(s).
Substring
Part of a string extracted with SUBSTRING(start, end, s).
Concatenation
Joining strings end to end with +.
Character code
The ASCII/Unicode number for a character; 'A' is 65.

Homework 1 min

Task (≤ 15 min): Write, in AQA pseudo-code, a program that inputs a word and outputs it in upper case followed by its length.

Model answer
OUTPUT 'Enter a word'
word ← USERINPUT
OUTPUT STRING_TO_UPPER(word)
OUTPUT LEN(word)

Award marks for: read the input (1), convert to upper case (1), output the length with LEN (1).