← Teacher guide

Working with text

Students tidy typed text with strip() and lower(), cut it into words with split(), pick words by place, and see why text must be tidied before it is compared. It is the first part of a text adventure.

Deeper Python in the console · Working with text · about 45 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 10 min

Before class

Read the unit primer first. Text has its own commands, called with a dot: command.strip() removes spaces at both ends, .lower() and .upper() change capitals, and .split() gives a list of words. Each gives back new text; the original is unchanged unless you store the result (command = command.strip()). They can be chained: input("> ").strip().lower(). The bug task is the reason they matter: "Yes" == "yes" is False. in checks whether something is in a list.

Running the lesson

  1. Predict: write " Go NORTH " on the board and cross off what strip() and lower() change, then count.
  2. Run: ask students to try to break it: extra spaces, capitals, one word, five words.
  3. Investigate: words[1] is the second word, as list places start at 0.
  4. Bug task: ask how many ways a person could type yes (Yes, YES, yes with a space).
  5. Make: word[0] is the first letter of a word, the same idea as words[0].

The hard part (on purpose)

Seeing why Yes and yes differ, and what split() gives back. Have them print the tidied text and the list of words before they fix the comparison.

When students get stuck

What you will seeA question to askThe fix
The program ignores a command that looks right.Was the text tidied before it was compared?Compare the tidied text: command.strip().lower() == "quit".
IndexError: list index out of range.How many words did split() give? Is there a words[1]?Check len(words) before using words[1].
The text did not change after strip() or lower().Was the result stored?command = command.lower(): the method gives back new text.

Answers to the checks

  1. Predict: What is the second line it prints?
    8. strip() removes the spaces at both ends and lower() makes the capitals small, so command is "go north": 2 letters, a space and 5 letters.
  2. Which word?: You type Take LAMP (with spaces around it). What is words[1]?
    lamp
    strip() and lower() turn it into "take lamp"; split() makes ["take", "lamp"], and place 1 is "lamp".
  3. Count the words: You type: pick up the red key. How many words does split() give?
    5
    split() cuts at every space: pick, up, the, red, key. Five words, so the program asks for two.
  4. Spot the bug: You type Yes and the door stays shut. Click the line that causes it.
    line 2
    "Yes" == "yes" is False: capitals count. Compare tidied text: answer.strip().lower() == "yes".
  5. Where this leads: A website's search finds "Python" when you type " python ". What has it done first?
    Tidied the text: removed the spaces and ignored capitals
    Almost every program that reads typed text tidies it first, exactly like strip() and lower().

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Quit

command = input("What do you do? ")
command = command.strip().lower()
words = command.split()
if command == "quit":
    print("Goodbye!")
elif len(words) == 2:
    print("Verb: " + words[0])
    print("Thing: " + words[1])
else:
    print("Try two words, such as: go north")

Modify › Known verbs only

command = input("What do you do? ")
command = command.strip().lower()
words = command.split()
if len(words) == 2:
    if words[0] in ["go", "take", "look"]:
        print("Verb: " + words[0])
        print("Thing: " + words[1])
    else:
        print("I don't understand " + words[0] + ".")
else:
    print("Try two words, such as: go north")

Make initials

name = input("What is your full name? ")
words = name.strip().split()
initials = ""
for word in words:
    initials = initials + word[0].upper()
print("Initials: " + initials)
print("In capitals: " + name.strip().upper())

Stretch and support

Stretch: Accept short commands too, such as n for go north, by checking a list of short forms.

Support: Give students the line that splits the name, and ask them to build the initials with a loop.

Exit ticket

What does " Hello World ".strip().lower().split() give?

["hello", "world"]: spaces off the ends, small letters, then cut into words.

What each step is for

Where it leads

Next lesson keeps an inventory in a list (append, remove, in). The parser from this lesson comes back in the text adventure.