Study Guide
String Handling
How a program works with text: finding the length of a string, picking out part of it with SUBSTRING, changing case with UCASE and LCASE, and using a loop to count vowels, reverse a word or build initials, with the difference between counting from 1 and from 0.
Lesson 5.8 Algorithms and Programming
For: Cambridge IGCSE Computer Science learners, or Key Stage 3 learners who have met loops and selection (about ages 13 to 14)
Stage: Think
A username from a name
A fictional school gives every pupil a computer username. The rule is: the first letter of the first name, followed by the first three letters of the surname, all in lower case.
Think it through
What is the username for a pupil called Amira Khan? Then say what a program would have to be able to do with the text “Amira” and “Khan” to follow the rule.
- Hint 1: Take the pieces one at a time, then join them.
- Hint 2: Which letters are wanted, and where in each name are they?
Show a model answer
The username is akha: “a” from Amira, then “kha” from Khan, in lower case. To follow the rule a program has to pick out letters from a given place in a piece of text (the first letter, the first three letters), join the pieces together and turn capital letters into lower case.
Stage: Discover
Where is each character?
Text in a program is a string: a row of characters, such as the letters of a word. Every character has a place in the row.
Think it through
In the word SUNSET, which place is the letter N in, counting from the left? How many characters does the whole word have?
- Hint 1: Count S as the first character.
Show what happens
N is the third character, and the word has 6 characters.
The number of characters in a string is its string length. Cambridge pseudocode finds it with LENGTH. The place of a character is its position, and the first character is at position 1. A substring is a part of a string, taken out with SUBSTRING(string, start, length): start is the position of the first character wanted, and length is how many characters to take.
Word ← "Computing"
OUTPUT LENGTH(Word)
OUTPUT SUBSTRING(Word, 1, 3)This outputs 9 (the word has nine characters) and then Com (three characters, starting at position 1). A string can include spaces and punctuation, and each counts as a character.
The third number in SUBSTRING is a length, not the position where the substring ends. SUBSTRING(Word, 4, 3) takes three characters starting at position 4, which are positions 4, 5 and 6. It does not mean “from position 4 to position 3”.
| Expression | Result |
|---|---|
| LENGTH(Word) | |
| SUBSTRING(Word, 1, 3) | Com |
| SUBSTRING(Word, 4, 3) | |
| SUBSTRING(Word, 7, 3) | |
| SUBSTRING(Word, 9, 1) |
Show the completed table
| Expression | Result |
|---|---|
| LENGTH(Word) | 9 |
| SUBSTRING(Word, 1, 3) | Com |
| SUBSTRING(Word, 4, 3) | put |
| SUBSTRING(Word, 7, 3) | ing |
| SUBSTRING(Word, 9, 1) | g |
The second row is given as an example. Each answer is a string, so its case matters when a program compares it.
Check your understanding
Show the answer
Answer: C. ne
Start at the position given, then take that many characters.
Stage: Explain
Capital letters count
A fictional quiz asks “Do you want to play again?” and moves on if the player types yes.
Think it through
One player types yes. Another types YES. A third types Yes. A program that tests Reply = “yes” accepts only the first. Why do you think a computer treats these as different, and how could the program accept all three?
- Hint 1: Think about how each letter is stored, as a code.
Show a model answer
A computer stores a capital Y and a small y as different characters, so “YES” and “yes” are not the same string, even though a person reads them as the same word. The program can change the reply to one case before it compares, so that all three become the same string.
UCASE returns a string with every letter in upper case, and LCASE returns it in lower case. They do not change the variable by themselves; they return a new string, which you store with an assignment.
INPUT Reply
Reply ← LCASE(Reply)
IF Reply = "yes"
THEN
OUTPUT "Starting again"
ELSE
OUTPUT "Goodbye"
ENDIFUCASE("Happy") gives HAPPY, and LCASE("Happy") gives happy. The lines Reply ← LCASE(Reply) and the test after it make YES, Yes and yes all give “Starting again”.
- Take the first letter of the first name.
SUBSTRING(First, 1, 1): start at position 1 and take one character, which isA. - Take the first three letters of the surname.
SUBSTRING(Surname, 1, 3): start at position 1 and take three characters, which areKha. - Join them. The
&operator joins two strings end to end:A&KhaisAKha. - Change the case last.
LCASEofAKhaisakha, the username. Doing it once at the end is simpler than changing each piece.
INPUT First
INPUT Surname
Start ← SUBSTRING(First, 1, 1)
Middle ← SUBSTRING(Surname, 1, 3)
Both ← Start & Middle
Username ← LCASE(Both)
OUTPUT UsernameCheck your understanding
Show the answer
Answer: B. Because YES, Yes and yes are different strings, so one test would match only one.
Change both sides of a comparison to the same case, so that capitals do not decide the answer.
Stage: Explain
Position 1, or position 0?
Think it through
In some countries the lift button for the ground floor is 0, and the next one up is 1. In others the ground floor is 1. If two people give directions (“second floor”), what could go wrong? What would help?
- Hint 1: Do both people start counting from the same place?
Show a model answer
They might mean different floors, because one counts the ground floor as the first and the other counts it as zero. It helps to agree where counting starts, or to give the name or number printed on the button.
Programming languages face the same question for the characters of a string. Cambridge pseudocode counts from 1: the first character is at position 1, so SUBSTRING("Hello", 2, 1) is e. Many real languages, including Python, count from 0: the first character is at index 0, so the e of “Hello” is at index 1. The syllabus notes that, in a program, the first character can be position zero or one, so always check which one a language or an exam question uses.
Do not confuse: Counting the characters of “Hello”
- First character HCambridge pseudocode
- Position 1. The e is at position 2.SUBSTRING(Word, 2, 1) gives e
- First character HPython
- Index 0. The e is at index 1.word[1] gives e
The characters are the same; only the numbering differs. The length of the string, 5, is the same in both.
Check your understanding
Show the answer
Answer: A. 3, because P is 0, l is 1, a is 2 and n is 3.
The same character gets a different number in each system; the characters themselves do not move.
Stage: Predict
A loop that looks at every character
To look at each character in turn, a loop counts positions from 1 to the length of the string, and takes out one character at a time with SUBSTRING(Word, Position, 1). A fictional spelling game wants to know how many vowels (a, e, i, o, u) are in a word.
Vowels ← 0
INPUT Word
FOR Position ← 1 TO LENGTH(Word)
Letter ← SUBSTRING(Word, Position, 1)
Letter ← LCASE(Letter)
IF Letter = "a" OR Letter = "e" OR Letter = "i" OR Letter = "o" OR Letter = "u"
THEN
Vowels ← Vowels + 1
ENDIF
NEXT Position
OUTPUT VowelsThink it through
Predict how many vowels the program counts for the word Cinema. Then say why the line Letter ← LCASE(Letter) is there.
- Hint 1: Count the vowels first. Then think about a word that starts with a capital letter.
Show what happens
Three: the i, the e and the a. The LCASE line makes a capital letter match the lower-case letters in the test, so that a word such as “Island” counts its capital I.
- Set the start. Vowels is 0 before the loop, as with any count.
- Work out the loop. Cinema has 6 characters, so Position goes 1, 2, 3, 4, 5, 6.
- Take one letter and tidy it. Position 1 gives
C; LCASE makes itc. It is not a vowel, so Vowels stays 0. - Test the vowels. Position 2 gives
i, which is a vowel, so Vowels becomes 1. Positions 4 (e) and 6 (a) add the other two. - Output. Vowels is 3.
Now trace the word Island yourself. The Letter column shows the letter after LCASE.
| Position | Letter after LCASE | Is it a vowel? | Vowels |
|---|---|---|---|
| Before the loop | 0 | ||
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 |
Show the completed table
| Position | Letter after LCASE | Is it a vowel? | Vowels |
|---|---|---|---|
| Before the loop | 0 | ||
| 1 | i | Yes | 1 |
| 2 | s | No | 1 |
| 3 | l | No | 1 |
| 4 | a | Yes | 2 |
| 5 | n | No | 2 |
| 6 | d | No | 2 |
The program outputs 2.
Check your understanding
Show the answer
Answer: D. 1, because the capital I no longer matches the lower-case i.
Change the case before comparing, or the test has to list both cases.
Stage: Explain
Building a new string
A program can build a new string by starting with an empty string (two quotation marks with nothing between them) and joining one character at a time on to it with &. To reverse a word, the loop must read the characters from the last to the first, which a FOR loop with STEP -1 does.
- Start empty.
Reversed ← "", because nothing has been built yet. - Count down.
FOR Position ← LENGTH(Word) TO 1 STEP -1visits positions 4, 3, 2, 1 for a four-letter word. - Add each letter to the end.
Reversed ← Reversed & Letter. Position 4 givess, so Reversed iss; position 3 givest, so Reversed isst; thensta; thenstab. - Output after the loop, when the new string is complete.
Reversed ← ""
INPUT Word
FOR Position ← LENGTH(Word) TO 1 STEP -1
Letter ← SUBSTRING(Word, Position, 1)
Reversed ← Reversed & Letter
NEXT Position
OUTPUT ReversedNow trace the same program for the word stop.
| Position | Letter | Reversed |
|---|---|---|
| Before the loop | (empty) | |
| 4 | ||
| 3 | ||
| 2 | ||
| 1 |
Show the completed table
| Position | Letter | Reversed |
|---|---|---|
| Before the loop | (empty) | |
| 4 | p | p |
| 3 | o | po |
| 2 | t | pot |
| 1 | s | pots |
The program outputs pots.
Joining strings is also called concatenation. The syllabus’s pseudocode guide lists the string functions of this lesson but does not name a symbol for joining. This lesson uses &, the symbol in Cambridge’s A Level pseudocode guide; in an exam, follow whatever the question shows. Python joins with +. Writing several values after OUTPUT, separated by commas, only displays them one after another: it does not build a new string.
Stage: Apply
Spot the mistake
Sam (fictional) wants to reverse a word but the program below outputs stop for the word stop.
Reversed ← ""
INPUT Word
FOR Position ← 1 TO LENGTH(Word)
Letter ← SUBSTRING(Word, Position, 1)
Reversed ← Reversed & Letter
NEXT Position
OUTPUT ReversedApply it
Show the answer
Answer: C. The letters are joined in the order read, first to last; count down with STEP -1.
Order matters: the order in which a loop reads the characters is the order in which they are joined.
Priya (fictional) knows Python well. She writes a pseudocode loop for the vowel program as FOR Position ← 0 TO LENGTH(Word) - 1, with the same SUBSTRING(Word, Position, 1) line.
Apply it
Show the answer
Answer: B. It asks for position 0, which does not exist, and never looks at the last character.
Use the counting of the language you are writing in, for the loop and for the substring together.
Stage: Apply
Write one yourself
A fictional school club needs initials for name badges. The program asks for a first name and a last name, typed in any case, and outputs the two initials in capitals.
Think it through
Write pseudocode that asks for a first name and a last name and outputs the initials in capitals. What should it output for maya and okafor?
Work this out on paper or the board first. Nothing here is marked: compare yours with the model answer.
- Hint 1: Take the first character of each name.
- Hint 2: Join the two letters, then change the case.
Show a model answer
OUTPUT "First name?"
INPUT First
OUTPUT "Last name?"
INPUT Last
FirstInitial ← SUBSTRING(First, 1, 1)
LastInitial ← SUBSTRING(Last, 1, 1)
Initials ← FirstInitial & LastInitial
Initials ← UCASE(Initials)
OUTPUT InitialsFor maya and okafor, the program outputs MO. Changing the case once, after joining, copes with names typed in any case. The two prompts tell the user what to type.
Go deeper The same ideas in Python
word = "Computing"
print(len(word))
print(word[0:3])
print(word[3:6])
print(word[8])
print(word.upper())
first = input("First name? ")
last = input("Last name? ")
initials = first[0] + last[0]
print(initials.upper())
vowels = 0
for letter in word.lower():
if letter in "aeiou":
vowels = vowels + 1
print(vowels)len(word)isLENGTH(Word), and it gives 9 forComputing.- Python counts from 0, so the first character is
word[0]and the last isword[len(word) - 1]or, more simply,word[-1]. - A slice such as
word[3:6]takes the characters from index 3 up to, but not including, index 6, which givesput. To convert,SUBSTRING(Word, start, length)isword[start - 1 : start - 1 + length]:SUBSTRING(Word, 4, 3)becomesword[3:6]. word.upper()andword.lower()areUCASEandLCASE. Joining strings uses+.- Python can loop straight over the characters (
for letter in word), so it does not always need a position. A string reversed withword[::-1]is a shortcut; the exam’s loop-and-join method is the one to practise.
Stage: Consolidate
Show what you know
Think it through
Explain to a friend how a program picks out part of a string, and what is different when you do it in Python. Use a word of your own as an example.
Show a model answer
A good answer says that SUBSTRING takes a start position and a length, with the first character at position 1 in Cambridge pseudocode, and gives an example, such as SUBSTRING("Planet", 2, 3) being lan. It says Python counts from 0 and uses a slice that stops before its end, so the same part is word[1:4]. It may add that LENGTH gives the number of characters and that LCASE and UCASE settle the case before a comparison.
Apply it
Show the answer
Answer: A. SUBSTRING(Word, LENGTH(Word), 1)
In Cambridge pseudocode the last position equals the length, because counting starts at 1.
- LENGTH(s) is the number of characters. SUBSTRING(s, start, length) takes
lengthcharacters starting at positionstart; the first character is at position 1 in Cambridge pseudocode. - UCASE and LCASE return upper- and lower-case versions; use them to compare text without capitals getting in the way, or to tidy output.
- A loop from 1 to
LENGTH(Word)withSUBSTRING(Word, Position, 1)looks at each character in turn. Counting down withSTEP -1reads from the end, which is how to reverse. - Python counts from 0 and uses slices that stop before the end, so
SUBSTRING(Word, 4, 3)isword[3:6]. - Check the usual faults: a start position of 0, a length taken for an end position, a case difference in a comparison, and joining letters in the wrong order.
Sources and scope
- What this lesson is. The eighth lesson of the Algorithms and Programming module, written for this site. It covers point 8.1.4(e) of Cambridge IGCSE Computer Science 0478 (2026 to 2028, version 6), string handling with LENGTH, SUBSTRING, UCASE and LCASE, and the note that the first character can be position zero or one. Its curriculum links are provisional and imply no review or endorsement by any examination board.
- Original material. The school usernames, the quiz, the spelling game, the club badges, Sam and Priya are fictional, written for this lesson. Every output was worked out by hand and checked with an equivalent Python program.
- Simplified on purpose. Strings hold only ordinary characters. The joining operator
&is not named in the 0478 pseudocode guide; it is taken from Cambridge’s A Level guide and explained as such. Searching within a string, comparing alphabetical order, and character codes are not taught here. - Left to later lessons. Procedures, functions and library routines, and arrays of strings.
A sequence of precise steps that solves a problem or completes a task.
A statement that stores a value in a variable. In Cambridge pseudocode it is written with an arrow, for example Score ← 0.
A loop that repeats a fixed number of times, known before it starts. In pseudocode it is a FOR … NEXT loop.
A set of instructions that is repeated, either a fixed number of times or until a condition says to stop.
A symbol or word that works on values to give a result, such as +, > or AND.
The text (or other input) given to an AI system to tell it what to do.
A precise but language-independent way of writing an algorithm, with set keywords such as IF, WHILE and OUTPUT, that is not tied to any one programming language.
A high-level programming language written as text, popular for learning and for real work, in which indentation shows which lines belong together.
The data type (STRING) for text: zero or more characters written between double quotes, such as “Hello”.
The number of characters in a string, found with LENGTH.
A part of a string, taken out with SUBSTRING by giving the start position and the number of characters.
A named place in memory that stores a value which can change while a program runs.
Your progress on this lesson
Saved in this browser only. It is not sent anywhere, does not follow you to another device, and is lost if you clear this site’s data.
This browser is not allowing the site to save progress, so it will be forgotten when you leave the page.