[8.1.4e] String Handling
[8.1.4e] String Handling
Strings are sequences of characters such as letters, digits, punctuation, and spaces. In IGCSE programming you regularly need to find the length of a string, take a substring, change case to upper or lower, and understand where positions start from. Mastering these operations helps you validate input, display tidy output, and extract useful information from text.
Strings as sequences
A string behaves like an ordered row of boxes, one box per character. Each box has a position (also called an index). Some languages start counting positions at 0, others at 1. Exam pseudocode for Cambridge uses positions starting at 1 when functions like LENGTH and SUBSTRING are used. Python, which many schools use for practice, starts positions at 0. This difference is important for getting the right characters and avoiding off-by-one errors.
Core string operations
These are the four core operations you must know for this benchmark. Explore them in the tabs below using clear, contrasting examples and edge cases.
Length() with mixed characters
LENGTH counts every character: letters, spaces, digits, and punctuation. The length is not the largest position; it is the number of boxes in the string.
// CAIE Pseudocode
DECLARE s : STRING ← "CS 0478!"
OUTPUT LENGTH(s) // 8
# Python
s = "CS 0478!"
print(len(s)) # 8
Here there are 8 characters including the space and the exclamation mark. When validating minimum or maximum lengths for usernames or postcodes, always include spaces if the specification says so.
Substring selection: start & length
SUBSTRING extracts a portion of the original string. In CAIE pseudocode the common form is SUBSTRING(text, start, length), where start is 1-based. In Python slicing is written as text[start:end] where start is 0-based and end is one past the last character required.
// CAIE Pseudocode
DECLARE city : STRING ← "Cambridge"
OUTPUT SUBSTRING(city, 1, 3) // "Cam"
OUTPUT SUBSTRING(city, 5, 3) // "rid"
// Python
city = "Cambridge"
print(city[0:3]) # "Cam"
print(city[4:7]) # "rid"
Keep the different parameter meanings in mind: CAIE uses start plus length; Python uses start and end-exclusive indices.
Changing case: UPPER vs lower
Changing case is essential for case-insensitive comparisons. Convert both strings to the same case before comparing. This avoids false mismatches, e.g. "Yes" vs "yes".
// CAIE Pseudocode
DECLARE answer : STRING
INPUT answer
IF UPPER(answer) = "YES" THEN
OUTPUT "Proceed"
ELSE
OUTPUT "Cancelled"
ENDIF
# Python
answer = input()
if answer.upper() == "YES":
print("Proceed")
else:
print("Cancelled")
Use UPPER and LOWER to normalise input. Be consistent across your programme for predictable behaviour.
Indexing schemes: 0-based vs 1-based (and edge cases)
Some environments count starting at 1, others at 0. The table shows positions for the word COMPSCI. Understanding this mapping prevents off-by-one errors when converting between pseudocode and a real language like Python.
| Character | 1-based position (CAIE) | 0-based index (Python) |
|---|---|---|
| C | 1 | 0 |
| O | 2 | 1 |
| M | 3 | 2 |
| P | 4 | 3 |
| S | 5 | 4 |
| C | 6 | 5 |
| I | 7 | 6 |
// CAIE Pseudocode
DECLARE w : STRING ← "COMPSCI"
OUTPUT SUBSTRING(w, 1, 1) // "C" first character
OUTPUT SUBSTRING(w, LENGTH(w), 1) // "I" last character
# Python
w = "COMPSCI"
print(w[0]) # "C" first character (index 0)
print(w[len(w)-1]) # "I" last character
When converting algorithms: subtract 1 from CAIE start positions to get Python start indices, and convert CAIE length to an end index by adding start-1+length.
Applying string handling in real tasks
- Input validation: check a password length is at least 8, or a UK mobile number has exactly 11 digits (ignore spaces if specified).
- Formatting names: output names in title case, or create initials like A.B. by taking the first character of each part.
- Searching: convert both the search term and the source text to the same case before comparing.
Deep Dive: Translating between CAIE pseudocode and Python
In assessment you may write CAIE-style pseudocode such as SUBSTRING(text, start, length). When practising in Python, translate using the rule text[start-1 : (start-1)+length]. For example, CAIE SUBSTRING("Cambridge", 5, 3) becomes Python "Cambridge"[4:7]. Remember that Python excludes the end index.
Common pitfalls and how to avoid them
- Counting from the wrong start: write a comment stating whether you are using 0-based or 1-based positions and stick to it.
- Forgetting spaces count: when measuring LENGTH, plan whether to strip spaces first. Only remove spaces if the specification requires it.
- Incorrect substring bounds: double-check that the substring stays within the string. Use
LENGTH(text)to compute safe lengths. - Case-sensitive comparisons: normalise with UPPER or LOWER before comparing user input to fixed words.
Key terminology
- String: an ordered sequence of characters.
- Length: the number of characters in a string.
- Substring: part of a string selected by position and length.
- Upper/Lower case: operations that convert letters to all capitals or all small letters.
- Index: a position used to access characters in a string. May start at 0 or 1 depending on the language or notation.
Key Takeaways
- LENGTH counts all characters including spaces and punctuation; the empty string has length 0.
- SUBSTRING in CAIE uses start (1-based) and length; Python uses start:end with 0-based indices and an end that is excluded.
- Use UPPER/LOWER to make comparisons case-insensitive and to standardise user input.
- Know whether the first character position is 0 or 1 in your chosen environment to avoid off-by-one errors.
- String handling underpins real tasks such as validation, searching, and formatting names or codes.