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.
Strings are indexed — counting from zero
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
Most string work is one of four moves: measure it, grab a piece, join pieces, or change the case.
| Operation | Meaning | Example on "HELLO" |
|---|---|---|
| Length | How many characters | 5 |
| Substring | Extract part of it | chars 0–2 → "HEL" |
| Concatenation | Join strings together | "HELLO" + " THERE" |
| Change case | Upper 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.
"PATEL" and year 7:
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."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.