Strings

💻 Computer Science Years 8–11 Foundation

How programs store, measure, slice and join text using zero-based indexing and core string operations.

Open this lesson, with practice and a printable worksheet →

Type your name into almost any app and something clever happens: the computer treats those letters as a single object it can slice, measure, join and search. That object is a string — an ordered sequence of characters — and knowing how to take one apart is one of the most-used skills a programmer has.

Why this matters: Nearly every program handles text — usernames, postcodes, passwords, search boxes, file names. Validating a login, formatting a date, or generating an email address all come down to string manipulation. Exam questions love it because it tests loops, indexing and functions all at once.
HELLO 01234 index (position) of each character
A string is a row of boxes; each character has an index starting at 0.

Strings are indexed — counting from zero

the first locker is number 0, not 10123firstfourth
Zero-based indexing: the first position is numbered 0.

Think of a string as a row of numbered lockers. The first character sits in locker 0, not 1. So in "HELLO", position 0 holds H and position 4 holds O. This zero-based indexing catches nearly everyone out at first, but it is the same across almost every language, so it is worth getting into your head early. Because the count starts at zero, a five-character string only ever reaches index 4 — the length and the last index are always one apart.

The four jobs you will always need

LengthHELLO → 5SubstringHELLOConcatenateHI+YAChange casehi ↔ HI
The four core moves: measure, slice, join, re-case.

Most string work is one of four moves: measure it, grab a piece, join pieces, or change the case.

OperationMeaningExample on "HELLO"
LengthHow many characters5
SubstringExtract part of itchars 0–2 → "HEL"
ConcatenationJoin strings together"HELLO" + " THERE"
Change caseUpper or lower"hello"

An analogy: a train of carriages

Picture a string as a train. Each carriage is one character, coupled in order. Concatenation is coupling two trains into one longer train; a substring is uncoupling a few carriages to run on their own. You can count the carriages (length) but you cannot leave gaps — the order is fixed.

Worked example — building a username. A school wants usernames from the first 3 letters of the surname plus a year number. For surname "PATEL" and year 7:
substring(0,3) then concatenate "7"PATEL01234PAT7
Slice the first three letters, then join the year to make PAT7.
surname = "PATEL"
part    = surname.substring(0, 3)   # "PAT"
username = part + "7"               # "PAT7"
Take characters at index 0, 1, 2 (three letters), then concatenate the year on the end.
Common mistake: forgetting that indexing starts at 0. Asking for “character 5” of "HELLO" (length 5) reaches past the end — the last valid index is 4. Reaching outside the string causes an index out of range error.

Tip: when a question mixes length and index, remember the last index is always length − 1. Sketch the boxes with their numbers before you write a single line of code.

Get the printable worksheet →
← Arrays & Lists → Functions & Procedures
← Sasta Basta home All subjects