In this topic students learn to take strings apart and build new ones: measuring them, changing their case, slicing out substrings, looping over their characters and converting between characters and ASCII codes.
Be able to read, comprehend, trace, adapt and create Python code that:
lenord and chrThis chapter was written for Dodona from new material by the course author, after the rest of the course had been converted, so it has no deck of its own. The author’s Intermediate Python series has a String manipulation deck that covers the same topic with different examples: on Google Slides. A PDF copy is kept on Dodona too.
This chapter comes after Lists and leans on it throughout. A string is indexed
the same way a list is, from zero, with square brackets, and len does the same
job on both. Students who are secure on list indexing usually find single
characters easy; the new idea is the slice, where two numbers pick out a whole
stretch.
The chapter is split into four short theory pages rather than one: length and case, substrings, looping through a string, and ASCII codes. The course author’s code-snippets table closes the last of them as a summary. Two of those pages teach constructs that the author’s tasks need but the snippets table does not list: iterating over a string (Make 2 and Make 3) and nested loops (Make 3).
Later chapters rely on this one: 2D arrays uses upper() and file handling uses
lower().
len counts every character, spaces and punctuation included.upper() and lower() return a new string. On its own, name.upper()
changes nothing: the result has to be stored or printed.isupper() and islower() are both False for a string with a mix of
capitals and small letters. Characters without a case are ignored.string[a:b] includes the character at a and stops before b,
so it is b - a characters long. This is the same rule as the stop value of
range. The snippets table writes it as
string[start_position:end_position + 1], where end_position is the last
character wanted.string[:n] is the first n characters, string[-n:] the
last n. The only difference on the page is which side of the number the
colon is on, which is the point the course author asks students to pay
attention to.ord turns one character into a number and chr turns a number into one
character. Arithmetic happens on the number, never on the character.for i in range(len(word)) visits every position. for letter in word visits
every character but not its position, so it cannot be used to find where a
character is.name[2] of Turing is r, not u. The predict task
asks for exactly this, and the spoiler names the wrong answer so students
who predicted 117 can see where it came from.text[20:26] to include the
character at 26. Checking that the length of a slice is the second number
minus the first is a quick self-check worth drilling.text[:6] of the wolf sentence ends in a space, and
text[-8:] gives ely wolf rather than the whole last two words. Both come up
in the predict task.ord("6") is 54. This is worth
connecting back to int(): typed input is characters until it is cast.word.upper() on a line of its own does
nothing. In the vowel counter the test suite includes words in small and mixed
letters, so a missing conversion shows up as a wrong count.> against >=. “8 or more” in the password task includes 8, and the
suite tests a password of exactly 8 characters.print indented under
the inner loop runs once per character, or once per space, instead of once per
name. Tracing the two loops on the board with one short name is worth the
time.