Study Guide Instructor view
String Handling
Teaching notes for Lesson 5.8, with controls for presenting it.
Keep this page on your own screen. Share only the lesson window. The lesson window holds no teaching notes, so sharing it cannot show them.
Presenting
- Open the lesson in its own window and share that window, not this one.
- Use Next and Previous here, or the lesson’s own, to move through the lesson. Next reveals one thing at a time: the next idea, or the answer to the question on screen, then the next step. Previous takes the last reveal back. The line below says what is on screen and what Next will show.
- Reveal hints and answers when the class is ready, and reset a demonstration before using it again.
The controls need JavaScript and a browser that supports BroadcastChannel. Without them, open the lesson in presentation mode and use its own controls.
The two windows talk to each other only inside this browser, on this device. Nothing is sent to a server, and no account is needed. A second device (a phone or another tablet) cannot follow the lesson window yet.
Now on the lesson window
- On screen
- Next teaching point
Guidance for this point
Lesson plan
- Level
- Intermediate
- Time
- About 75 minutes; the steps below add up to 75
Pupils start from a school username rule (first letter of the first name plus three letters of the surname) and discover for themselves the three things a program needs: pick letters from a place, join, and change case. They then meet LENGTH and SUBSTRING (with start and length, counting from 1), UCASE and LCASE, the clash between counting from 1 in Cambridge pseudocode and from 0 in Python, a loop that looks at every character, and building a new string by joining. Tracing short words on the board is the main tool, because nearly every error is a position or order slip.
Objectives
- Use LENGTH, SUBSTRING, UCASE and LCASE to work out the result of a string expression
- Explain that Cambridge pseudocode counts the first character as position 1, and that Python counts from 0
- Write and trace a loop that looks at each character of a string in turn
- Count vowels, reverse a string and build initials using string handling and a loop
- Spot and correct mistakes in string-handling programs
Before the lesson
- Mini whiteboards, one per pupil, for predictions and for writing positions under letters.
- Write the words Computing, Cinema, Island, stop and Planet on the board with a number under each letter (1, 2, 3 ...) so that position and length are visible.
- Optional: letter cards for one word that pupils can arrange, and a second row of cards numbered from 0 for the Python comparison.
- Copy the vowel and reverse programs onto the board before the lesson so you can trace them with the pen.
Steps
Introduction Opening
On the lesson window now
On screen: the lesson’s title, module and number; Next adds its objectives and key terms, as the lesson page lists them; Next again begins the first step.
Objectives on screen
- Use LENGTH, SUBSTRING, UCASE and LCASE to work out the result of a string expression
- Explain that Cambridge pseudocode counts the first character as position 1, and that Python counts from 0
- Write and trace a loop that looks at each character of a string in turn
- Count vowels, reverse a string and build initials using string handling and a loop
- Spot and correct mistakes in string-handling programs
Purpose: Pupils start from a school username rule (first letter of the first name plus three letters of the surname) and discover for themselves the three things a program needs: pick letters from a place, join, and change case. They then meet LENGTH and SUBSTRING (with start and length, counting from 1), UCASE and LCASE, the clash between counting from 1 in Cambridge pseudocode and from 0 in Python, a loop that looks at every character, and building a new string by joining. Tracing short words on the board is the main tool, because nearly every error is a position or order slip.
1. A username from a name Think 4 min
On the lesson window now
Purpose: Pupils work out a username from a rule and say what a program would have to be able to do with text, before any function is named.
Ask
- What is the username for Amira Khan?
- What would a program have to be able to do with Amira and Khan to follow the rule?
Responses to expect
- akha
- Pick letters from a place, join the pieces, and change capitals to lower case
Misconceptions to challenge
- A learner may believe: “The username is the first four letters of the full name.” Respond: Read the rule again: first letter of the first name, then three letters of the surname.
Checking understanding: Pupils write the username on a mini whiteboard and hold it up; check for akha, not aamk or Akha.
Depending on the answers
- If pupils write AKha or Akha: ask which part of the rule says lower case and what the program would have to do to the capitals.
English as an additional language
- Check first name, surname and lower case; show Amira Khan on cards.
Discussion pause: Thirty seconds of silent thinking, then pairs write the username before anyone shares.
In the lesson window
- 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.
2. Where is each character? Discover 10 min
On the lesson window now
Purpose: Pupils find the position of a letter and the length of a word, then meet LENGTH and SUBSTRING and work out the results of several calls on a nine-letter word.
Ask
- Which place is N in SUNSET, and how many characters does it have?
- What does SUBSTRING(Word, 4, 3) take from Computing?
- What does SUBSTRING("Cinema", 3, 2) return?
Responses to expect
- Third, and 6 characters
- put
- ne
Misconceptions to challenge
- A learner may believe: “The third number in SUBSTRING is where to stop.” Respond: It is how many characters to take. SUBSTRING(Word, 4, 3) is positions 4, 5 and 6, not from 4 to 3.
- A learner may believe: “The first character is at position 0.” Respond: In Cambridge pseudocode it is at position 1; position 0 is for some other languages, later in this lesson.
- A learner may believe: “SUBSTRING("Cinema", 3, 2) is nema because it takes everything from position 3.” Respond: The length is 2, so only two characters are taken.
- A learner may believe: “Spaces do not count as characters.” Respond: Each space and punctuation mark is a character and has a position.
On the board or with the pen
- Write C o m p u t i n g with 1 to 9 under each letter and bracket the characters for each call as pupils say them.
What learners should notice
- The result of each SUBSTRING is a string, so case matters when comparing.
Checking understanding: The substring-cinema check: em is the counting-from-0 slip, nema is taking all the letters to the end. Pupils who choose em or nema have misread start or length.
Depending on the answers
- If pupils answer em: ask which position holds C in Cinema and where Cambridge pseudocode starts counting.
- If pupils answer nema: ask which of the three numbers says how many characters to take.
- If pupils are secure: ask them to write the call that gives dog from Underdog.
Support
- Give pupils the word with positions numbered underneath for the first two rows of the table.
Stretch
- What does SUBSTRING(Word, 5, 10) do on Computing? Why is the length too big?
English as an additional language
- Check position, length and character; point to each letter as you count.
In the lesson window
- 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?
- Check your understanding: What does SUBSTRING(“Cinema”, 3, 2) return?
3. Capital letters count Explain 11 min
On the lesson window now
Purpose: Pupils work out why YES, Yes and yes are different to a computer, meet UCASE and LCASE, then build the username step by step in a worked example.
Ask
- Why does a program treat yes, YES and Yes as different, and how could it accept all three?
- Why does the quiz change Reply to lower case before the IF test?
- In the username program, why is LCASE done last?
Responses to expect
- Each letter is stored as a different code for capital and small
- Convert to one case first
- Doing it once at the end is simpler than changing each piece
Misconceptions to challenge
- A learner may believe: “LCASE changes the variable by itself.” Respond: It returns a new string; you must store it with an assignment, as in Reply ← LCASE(Reply).
- A learner may believe: “INPUT only accepts lower case.” Respond: Players can type capitals; that is why the quiz must handle them.
- A learner may believe: “LCASE shortens the string.” Respond: It changes letters, not the number of characters.
- A learner may believe: “& and OUTPUT with commas do the same thing.” Respond: & builds one new string; commas on OUTPUT only display values one after another.
On the board or with the pen
- Write YES, Yes and yes in a column with arrows through LCASE all pointing to yes. Then build akha in four lines: A, Kha, AKha, akha.
Checking understanding: The case-compare question: INPUT-only-lower-case and shorter-string are the two common wrong reasons; the correct option names the real problem, that three spellings are three strings.
Depending on the answers
- If pupils think LCASE changes the variable: write Reply ← LCASE(Reply) and ask which part stores the answer.
- If pupils are secure: ask what happens if only the typed reply is converted and the comparison string is YES.
Support
- Provide the username steps with the SUBSTRING calls written and ask pupils to fill in the results.
Stretch
- Make the username program cope with a surname shorter than three letters. What does SUBSTRING give?
English as an additional language
- Check upper case, lower case and capital; show a capital and a small letter side by side.
In the lesson window
- 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?
- Check your understanding: Why does the quiz change Reply to lower case before the IF test?
4. Position 1, or position 0? Explain 8 min
On the lesson window now
Purpose: Pupils see from a lift analogy that counting can start at 0 or 1, then apply it to a word in a language that counts from 0.
Ask
- What could go wrong if two people give floor numbers in different systems?
- In a language that counts from 0, which position holds the n in Planet?
Responses to expect
- They could mean different floors
- 3, because P is 0, l is 1, a is 2, n is 3
Misconceptions to challenge
- A learner may believe: “Position 4 is right because n is the fourth letter.” Respond: That is the position counting from 1. Which number does this language give the first letter?
- A learner may believe: “The characters move when the numbering changes.” Respond: The characters stay put; only the numbers differ.
- A learner may believe: “The length of the string changes with the numbering.” Respond: The length of Hello is 5 in both systems.
On the board or with the pen
- Draw Planet twice, once with 1 to 6 under the letters and once with 0 to 5, and circle n in both.
Checking understanding: The first-position check: 4 is the counting-from-1 answer, so it shows pupils who have not switched systems.
Depending on the answers
- If pupils choose 4: ask them to number the letters from 0 and read off n.
- If pupils are secure: ask them what the last position of Planet is in each system.
English as an additional language
- Check ground floor and first floor, which differ in different countries.
In the lesson window
- 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?
- Check your understanding: A program is written in a language that counts the first character as position 0. In the word “Planet”, which position holds the letter n?
5. A loop that looks at every character Predict 11 min
On the lesson window now
Purpose: Pupils predict the vowel count for Cinema, follow a worked trace, trace Island themselves, and find what happens when LCASE is removed.
Ask
- How many vowels does the program count for Cinema, and why is the LCASE line there?
- What does the program output for Island with the LCASE line removed?
Responses to expect
- Three: i, e, a
- A capital I would be missed
- 1, because the capital I no longer matches
Misconceptions to challenge
- A learner may believe: “The capital I still counts without LCASE.” Respond: The test compares with lower-case letters; I is a different character from i.
- A learner may believe: “No vowel is counted without LCASE.” Respond: The lower-case a still matches.
- A learner may believe: “The loop should start at 0.” Respond: Positions run from 1 to LENGTH(Word); the loop uses those.
- A learner may believe: “SUBSTRING(Word, Position, 1) takes the rest of the word.” Respond: The 1 means one character per pass.
On the board or with the pen
- Trace Island with Position, Letter after LCASE, Is it a vowel? and Vowels; then redo with the letters as typed and a cross on the capital I.
Checking understanding: The vowel-line check has four options: 0, 2, 3 and the correct 1; each wrong one corresponds to a different belief about what LCASE does.
Depending on the answers
- If pupils answer 2: ask them to compare the capital I and the string i letter by letter.
- If pupils trace quickly: ask them to add consonants and count them in the same loop.
Support
- Provide the Island table with the Letter column filled in so that pupils decide only whether each letter is a vowel.
Stretch
- Rewrite the test to cope with capitals and no LCASE. How long does the condition become?
English as an additional language
- Check vowel and consonant; say each letter aloud as you trace.
In the lesson window
- Think it through: Predict how many vowels the program counts for the word Cinema. Then say why the line Letter ← LCASE(Letter) is there.
- Check your understanding: The program is run on the word Island with the line Letter ← LCASE(Letter) removed. What does it output?
6. Building a new string Explain 8 min
On the lesson window now
Purpose: Pupils see how a new string is built from an empty one, follow the worked reversal of bats, and trace stop themselves.
Ask
- What does Reversed hold before the loop, and after each pass?
- Why does the loop count down from LENGTH(Word) to 1?
- What does the program output for stop?
Responses to expect
- Empty, then p, po, pot, pots
- Reading from the end makes the letters join in the opposite order
- pots
Misconceptions to challenge
- A learner may believe: “Reversed ← Reversed & Letter puts the new letter at the start.” Respond: & joins on the end: p with o joined on is po.
- A learner may believe: “The loop should go from 1 to the length for any string job.” Respond: Counting down with STEP -1 reads from the end.
- A learner may believe: “OUTPUT with commas builds the new string.” Respond: It only displays; building needs &.
On the board or with the pen
- Write the four passes in a column: Position 4 p, 3 o, 2 t, 1 s, with Reversed growing as p, po, pot, pots.
Checking understanding: The reverse-trace table checks that each join goes on the end.
Depending on the answers
- If pupils write op, to, ts: ask them which end the new letter joins and trace the first two passes again.
- If pupils are secure: ask for a program that outputs the word with each letter twice.
Support
- Provide the first row of the trace and ask pupils to continue.
English as an additional language
- Check empty string and join; show a train adding carriages at the back.
7. Spot the mistake Apply 8 min
On the lesson window now
Purpose: Pupils find why Sam's reverse program outputs stop for stop, and what goes wrong when Priya's loop starts at 0 in Cambridge pseudocode.
Ask
- What is the fault in Sam's reverse program and what is the fix?
- What goes wrong with FOR Position ← 0 TO LENGTH(Word) - 1?
Responses to expect
- The letters are read first to last and joined in the same order; count down with STEP -1
- Position 0 does not exist and the last character is never looked at
Misconceptions to challenge
- A learner may believe: “The loop runs one position too far.” Respond: The loop covers all four positions; trace it: the letters come out in the original order.
- A learner may believe: “Reversed should start as Word.” Respond: Then the word is joined on to itself; it should start empty.
- A learner may believe: “Nothing goes wrong because Python starts at 0.” Respond: The loop is for Cambridge pseudocode, where positions start at 1.
- A learner may believe: “Only the loop needs changing and SUBSTRING takes position 0.” Respond: The numbering belongs to SUBSTRING as well.
On the board or with the pen
- Trace Sam's loop for stop: s, st, sto, stop. Then trace Priya's loop and mark the failing call SUBSTRING(Word, 0, 1) and the missing last position.
Checking understanding: The reverse-fault and position-zero questions: each wrong option corresponds to an off-by-one, a wrong starting value or a wrong language belief.
Depending on the answers
- If pupils choose the shorter loop: trace the first pass of Sam's program and ask which letter joins first.
- If pupils are secure: ask which two numbers in the loop header make the program correct for Cambridge pseudocode.
Support
- Provide a trace table with Position and Reversed columns for stop.
Stretch
- Which loop header would be correct if the language counted from 0?
In the lesson window
- Apply it: What is the fault, and what is the fix?
- Apply it: What goes wrong when Priya’s loop is used in Cambridge pseudocode?
8. Write one yourself Apply 7 min
On the lesson window now
Purpose: Pupils write a short program that outputs initials in capitals, with prompts, and predict its output for maya and okafor.
Ask
- Write pseudocode that outputs the two initials in capitals.
- What does it output for maya and okafor?
Responses to expect
- SUBSTRING(First, 1, 1) and SUBSTRING(Last, 1, 1), joined with &, then UCASE
- MO
Misconceptions to challenge
- A learner may believe: “UCASE changes only the first letter.” Respond: UCASE returns the whole string in upper case; here the string is two letters.
- A learner may believe: “The initials are joined with a comma in OUTPUT.” Respond: Commas display values one after another; & builds one string.
- A learner may believe: “UCASE(First) alone gives the initial.” Respond: It gives the whole name in capitals; SUBSTRING picks the letter.
On the board or with the pen
- Write the program as pupils dictate it, then trace maya and okafor line by line.
Checking understanding: Collect the pseudocode: look for the SUBSTRING start and length, the join, UCASE, and the two prompts.
Depending on the answers
- If pupils output mo: ask which line should change the case and where it should be.
- If pupils are quick: ask them to output the initials with a full stop after each.
Support
- Provide the lines that get the two initials and ask pupils to write the rest.
Stretch
- Add a third initial from a middle name typed by the user.
In the lesson window
- 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?
9. Show what you know Consolidate 8 min
On the lesson window now
Purpose: Pupils explain how a program picks part of a string and what differs in Python, then apply the last-position idea before the summary.
Ask
- Explain how SUBSTRING picks out part of a string, with a word of your own, and what differs in Python.
- Which expression gives the last character of Word, whatever the length?
Responses to expect
- A start position and a length, counting from 1
- Python counts from 0 and its slice stops before the end
- SUBSTRING(Word, LENGTH(Word), 1)
Misconceptions to challenge
- A learner may believe: “The last character is at LENGTH(Word) - 1.” Respond: Try Cat: its length is 3, so position 2 is a. In Cambridge pseudocode the last position equals the length.
- A learner may believe: “SUBSTRING(Word, 1, LENGTH(Word)) gives the last character.” Respond: It takes the whole word from position 1.
Checking understanding: Collect the explanations: check for start and length, the 1 or 0 difference, and an example.
Depending on the answers
- If pupils choose LENGTH(Word) - 1: write Cat with positions 1, 2, 3 and ask where the last letter is.
- If several are unsure of the difference from Python: start the next lesson by converting SUBSTRING(Word, 4, 3) to a Python slice.
In the lesson window
- 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.
- Apply it: A program holds the string Word and needs its last character, whatever the length. Which expression gives it?
Lesson recap Closing
On the lesson window now
On screen: the lesson’s objectives again; Next adds the closing question, which asks the class to explain each one in their own words without looking back. It says nothing about what anyone has learnt: use the answers to decide what to revisit.
Points to listen for
- LENGTH is the number of characters; SUBSTRING(s, start, length) takes length characters from position start, with the first at position 1 in Cambridge pseudocode.
- UCASE and LCASE return new strings; change the case so that capitals do not decide a comparison.
- A loop from 1 to LENGTH(Word) with SUBSTRING(Word, Position, 1) looks at every character; counting down with STEP -1 reads from the end.
- Python counts from 0 and its slices stop before the end.
- Usual faults: a start of 0, a length mistaken for an end, case differences, joining in the wrong order.
Consolidation
- LENGTH is the number of characters; SUBSTRING(s, start, length) takes length characters from position start, with the first at position 1 in Cambridge pseudocode.
- UCASE and LCASE return new strings; change the case so that capitals do not decide a comparison.
- A loop from 1 to LENGTH(Word) with SUBSTRING(Word, Position, 1) looks at every character; counting down with STEP -1 reads from the end.
- Python counts from 0 and its slices stop before the end.
- Usual faults: a start of 0, a length mistaken for an end, case differences, joining in the wrong order.