The shape of the lesson
By the end of the lesson, learners can
- Length and case: In the lab, choose the String handling program.
- SUBSTRING: SUBSTRING needs three things: the string, the position to start at, and how many characters to take.
- Strings in a loop: To look at every character, loop from 1 to LENGTH and take a substring of length 1 each time.
- Write positions under each character, starting at 1. Spaces count.
- SUBSTRING(s, start, length): the third value is how many characters, not where to stop.
- Case: convert with UCASE or LCASE before comparing.
- Every character: FOR Index ← 1 TO LENGTH(s) with SUBSTRING(s, Index, 1).
One picture every idea returns to
A string is a sequence of characters. LENGTH(s) returns the number of characters, spaces included. SUBSTRING(s, start, length) returns length characters starting at position start. UCASE and LCASE return the string in upper or lower case. In 0478 pseudocode the first character is generally at position 1; many programming languages, including Python, start at 0.
Forty-five minutes, phase by phase
| Time | Phase | What happens in the room | Grouping |
|---|---|---|---|
| 0 to 5 min | Hook: predict | Open with the question in the title, Slice the string.. Names, passwords, postcodes and messages are all strings. Put the figure on the board: it is the one picture every idea in the lesson returns to. | Whole class, then pairs |
| 5 to 13 min | Teach and check: Length and case | In the lab, choose the String handling program. Project tab 5 of the lab (String handling) and let a student drive it while the class predicts. | Whole class, then pairs |
| 13 to 21 min | Teach and check: SUBSTRING | SUBSTRING needs three things: the string, the position to start at, and how many characters to take. Model the worked example Counting positions on the board, one line at a time. Your turn: What do SUBSTRING("Cambridge", 1, 3) and SUBSTRING("Cambridge", 5, 5) return? | Pairs |
| 21 to 30 min | Teach and check: Strings in a loop | To look at every character, loop from 1 to LENGTH and take a substring of length 1 each time. Model the worked example Dry run on the board, one line at a time. Your turn: What is output for the input Excellence? | Corners |
| 30 to 36 min | Check | The four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong. | Individual, then whole class |
| 36 to 43 min | Plenary | The exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording. | Individual, random call |
| 43 to 45 min | Exit and homework | Set the worksheet below (6 questions, 17 marks). Learners self-assess against the objectives before they leave. | Individual |
Protect the parts that carry the learning
The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.
Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.
Cut first if the build overruns: the last check in Strings in a loop, never the check or the plenary.
In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.
Corners
Put one answer or viewpoint in each corner of the room. Students move to the corner they agree with, justify the choice to a partner there, then hear one voice from each corner before the reveal.
The full structure, with its variations and the other twelve, is on the cooperative-learning page.
What to head off, and how
| Trap learners fall into | Teaching move that pre-empts it |
|---|---|
| Spaces and punctuation are characters too. | LENGTH("Hi there!") is 9, not 7. |
| The third value is a length, not an end position. | Characters 3 to 6 are SUBSTRING(Word, 3, 4), not SUBSTRING(Word, 3, 6). |
| Without LCASE the capital E at the start of Eleven would not equal 'e', and the program would output 2. | Return to Strings in a loop: ask a pair to state the correct version and show it on the lab before moving on. |
String handling: worksheet
6 questions, 17 marks. Written in the style of the papers; no past paper question is reproduced.
State the purpose of the string operations LENGTH and SUBSTRING.
Text ← "Data Science". State what is returned by (a) LENGTH(Text) (b) SUBSTRING(Text, 6, 3) (c) UCASE(SUBSTRING(Text, 1, 4)).
Write pseudocode that inputs a person's first name and outputs its first letter in upper case and the number of letters in the name.
Write pseudocode that inputs a sentence and outputs the number of words, assuming words are separated by single spaces.
A login program compares the username typed with the stored username "SMITH". A user types smith and is rejected. Explain why and how to fix it.
A student wants characters 3 to 6 of Word and writes SUBSTRING(Word, 3, 6). Explain the error and correct it.
Answer key and marking notes
- LENGTH returns the number of characters in a string.
- SUBSTRING returns part of a string: a given number of characters starting at a given position.
- (a) 12 (4 + 1 space + 7)
- (b) "Sci"
- (c) "DATA"
INPUT FirstNameInitial ← UCASE(SUBSTRING(FirstName, 1, 1))OUTPUT InitialOUTPUT LENGTH(FirstName)
Spaces ← 0andINPUT SentenceFOR Index ← 1 TO LENGTH(Sentence)IF SUBSTRING(Sentence, Index, 1) = " " THEN Spaces ← Spaces + 1- After the loop:
OUTPUT Spaces + 1
- String comparison is case sensitive: "smith" is not equal to "SMITH".
- Convert the input with UCASE (or both strings to the same case) before comparing.
- The third parameter is the number of characters, so this returns 6 characters (positions 3 to 8).
- Correct:
SUBSTRING(Word, 3, 4).
6 multiple-choice questions on this lesson
From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.
What is the value of LENGTH("Top Score 10")?
- A12
- B10
- C9
- D11
Word holds "KEYBOARD". What is output by OUTPUT SUBSTRING(Word, 4, 3)?
- ABOA
- BOAR
- CYBO
- DBOARD
Which expression gives the last character of the string variable Word?
- A
SUBSTRING(Word, LENGTH(Word), 1) - B
SUBSTRING(Word, 1, LENGTH(Word)) - C
SUBSTRING(Word, LENGTH(Word) - 1, 1) - D
LENGTH(SUBSTRING(Word, 1, 1))
The input is Alabama. What is output?
DECLARE Word : STRING
DECLARE Count : INTEGER
DECLARE Index : INTEGER
INPUT Word
Count ← 0
FOR Index ← 1 TO LENGTH(Word)
IF SUBSTRING(Word, Index, 1) = 'a'
THEN
Count ← Count + 1
ENDIF
NEXT Index
OUTPUT Count- A3
- B4
- C7
- D2
Name holds "mARIE". What is output?
OUTPUT UCASE(SUBSTRING(Name, 1, 1)), LCASE(SUBSTRING(Name, 2, LENGTH(Name) - 1))
- AMarie
- BmARIE
- CMARIE
- DMmarie
This program is meant to count the words in a sentence by counting the spaces.
DECLARE Sentence : STRING
DECLARE Words : INTEGER
DECLARE Index : INTEGER
INPUT Sentence
Words ← 1
FOR Index ← 1 TO LENGTH(Sentence)
IF SUBSTRING(Sentence, Index, 1) = ' '
THEN
Words ← Words + 1
ENDIF
NEXT Index
OUTPUT WordsWhich input shows that the program does not always give the right answer?- A
Hi there(two spaces between the words) - B
Hello - C
Good morning all - D
a b c d
Quiz key
T-o-p (3) + space (1) + S-c-o-r-e (5) + space (1) + 1-0 (2) = 12 characters. 10 leaves out the two spaces. 9 counts only the letters. 11 misses one of the spaces or one of the digits.
Counting from position 1, K-E-Y-B, so position 4 is B; taking 3 characters gives BOA. OAR counts from position 0, as Python does. YBO starts one place too early. BOARD takes every character from position 4 to the end instead of just 3.
With positions starting at 1, the last character is at position LENGTH(Word), and taking 1 character from there gives it. SUBSTRING(Word, 1, LENGTH(Word)) returns the whole word. LENGTH(Word) - 1 is the second-to-last position. LENGTH(SUBSTRING(Word, 1, 1)) is the number 1, not a character.
Alabama has the letters A, l, a, b, a, m, a. The comparison is with lower-case 'a', and a capital A is a different character, so only the three lower-case a's are counted: 3. 4 would need LCASE around the SUBSTRING. 7 is the length of the word. 2 misses the final character, as if the loop stopped at LENGTH(Word) - 1.
SUBSTRING(Name, 1, 1) is m, and UCASE makes it M. LENGTH(Name) - 1 is 4, so SUBSTRING(Name, 2, 4) is ARIE, and LCASE makes it arie. Together: Marie. mARIE is the original string unchanged. MARIE would need UCASE on the whole name. Mmarie wrongly starts the second part at position 1.
Hi, two spaces, there has 2 words but 2 spaces, so the program outputs 3. Hello has no spaces and gives 1, which is right. Good morning all has 2 spaces and gives 3, which is right. a b c d has 3 spaces and gives 4, which is right; single letters are still words.
What to project
The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.
Slides for this lesson
A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.