← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 11 · Lecture

Lecture: Strings in Depth & Text Processing

Introduction to Computer Science Free sample edition

Lecture script · about 15–20 minutes spoken · Week 11

Opening

Picture a public-health student with a column of survey replies. Some people typed Yes, some typed yes, and a few typed YES with a stray space in front. A person sees one answer. A program sees several different strings, because a capital letter and a space are real characters.

Or picture a business major pulling the price out of each line of a sales log. Either way, a program has to take text apart, tidy it, and put it back together.

You've been working with text since Week 3. You learned to index a string from 0, to slice it, and to measure it with len. In Week 9 you met methods on lists, called with a dot. In Week 10 you met the word immutable, for a tuple. This week those ideas meet. A string has its own methods, and because a string is immutable, every one of them hands you something new. Next week, in the module on files and exceptions, you'll read whole files of lines like the ones you take apart here.

Here's the plan. We'll call the string methods, catch the most common string bug, and take a line of the reading log apart and rebuild it. Then we'll search, replace, and count, build small text programs, and check a chatbot's claim with a run.

1. Ask a string to do work

Think of a copy shop. You hand the clerk a page and ask for it bigger, or in bold. The clerk hands you a fresh copy, and your original goes back in your folder exactly as it was.

Python's version of that counter is a string method. A string method is a function that belongs to a string and is called with a dot after it, such as upper in s dot upper; it hands back a new value and never changes the string itself. This week you'll meet nine of them: upper, lower, strip, split, join, replace, find, count, and startswith.

Here's the pinned example. The name s holds the text "Hello, World", with a comma and a space in the middle. The program prints one method call per line. Upper gives HELLO, WORLD in capitals. Lower gives hello, world in small letters. Count of the letter l gives 3, because Hello has two l's and World has one. Find of a capital W gives 7, which is a position, counting from 0. Replace, swapping each small o for a zero, gives Hell0, W0rld. Startswith of Hello gives True. Strip, on a word with spaces on both sides, gives the word with no spaces.

And here's the line that matters most. The very last line prints s itself, and it still displays Hello, World. Every method above built a new string and handed it to print. Not one of them touched s.

Pause and try this. After all those method calls, why does the last line still display Hello, World with its capital H?

Think about what each method handed back, and where it went.

Here's the answer. Each method built a new string and handed it to print, and s was never reassigned. A string can't be changed, so s is still the original.

A string method hands you a new string; it never changes the old one.

2. Keep the new value

Imagine a friend reads a sign aloud for you in another language and translates it as they go. If nobody writes the translation down, the sign in your hand is still in the other language. The translation existed for a moment, and then it was gone.

That's the most common string bug, and it has a name. The forgot-to-reassign bug is the logic error of calling a string method on a line of its own and expecting the string to change; the new string is lost unless it is assigned back to the name.

Here's the pinned program. The name s gets the text Hello. The next line calls s dot upper on a line by itself. The last line prints s. The run displays Hello, with a small e and a small l, not HELLO.

Python did run upper and built HELLO. But nothing on that line stored it, so Python threw it away. No error, wrong output: a logic error, the kind only a prediction catches.

The cure is one line. Write s equals s dot upper. Python works out the right side first, which is HELLO, and then the equals sign puts that new string into the name s. Now the last line displays HELLO.

Pause and try this. In the pinned program, the line with upper runs without any error. So why does the output still say Hello?

Ask where the new string went after upper built it.

Here's the answer. Upper built HELLO, but no name stored it, so it was thrown away. The fix is to assign it back, s equals s dot upper.

No home for the new string means no change at all.

3. Take a line apart and rebuild it

Think of a cashier glancing at one line of a receipt. In a second they've split it in their head: how many, what item, what price.

Programs do the same with split and join. Whitespace is the characters that show up as blank space when text is printed, such as an ordinary space, a tab, and the line break at the end of a line. Split is the string method that cuts a string into pieces and hands back a list of strings; with empty parentheses it cuts on whitespace. And join is the string method called on a separator that hands back one new string made from a list of strings, with the separator placed between each neighbouring pair.

Here's this week's stage of your reading log. The name entry holds one line of the log: day 3, colon, 25 pages. Split cuts it at each space, and the program displays a list of four strings: day, then 3 with its colon, then 25, then pages. The 25 is still text. So the program converts it with int, adds 5, and displays 30, because 25 plus 5 is 30. Then it displays the line in capitals, DAY 3: 25 PAGES. And the last line displays entry itself, still day 3: 25 pages, untouched.

To rebuild the line, put the new count back into the list as text with str, so the list holds the string 30. Then join the four parts with a single space as the separator. The run displays day 3: 30 pages, and the old entry still says 25.

Pause and try this. What type does split hand back, and what type does join hand back?

Say the direction out loud: from one string, and back to one string.

Here's the answer. Split hands back a list, and join hands back a string. That's why the reading log could change one item of the list and then join the pieces into a new line.

Split takes the text apart, and join puts it together again.

4. Know what split cuts on

Think of a paper cutter. It cuts where you line up the blade, and nowhere else. Line it up on the spaces, and a comma won't stop it.

A separator is the piece of text that sits between the parts when strings are joined, or that a string is cut on when it is split, such as a space, a comma, or a hyphen. Split with empty parentheses uses whitespace as its separator, never a comma.

Here's the pinned example. The string is a, comma, b, space, c. With empty parentheses, split cuts only at the one space. The run displays a list of two items: the first is a comma b, all in one piece, and the second is c.

Now hand split the comma itself, as its separator. The blade lines up on the comma instead. The run displays a list of two different items: a on its own, and then b space c, with the space kept inside.

Join uses a separator the same way, in the other direction. The pinned join with a hyphen as the separator, on a list of a, b, and c, displays one string: the three letters with a hyphen between each pair and none at the ends.

Pause and try this. With empty parentheses, how many items does split hand back for the string a, comma, b, space, c?

Count the spaces, not the commas.

Here's the answer. Two items. There's one space, so split cuts once, and the comma stays inside the first item.

Split cuts where you point it, not anywhere else.

5. Search with find

Think of a library catalogue. You ask where a book is, and it answers with a shelf number. If the book is missing, it doesn't say no. It shows a code that means "not on the shelves," and you have to know that code.

Find is the string method that hands back the position of the first match of the text it is given, counting from 0, or minus 1 when there is no match; it never hands back True or False.

In the pinned method set, find of a capital W gives 7, and find of a small z gives minus 1. And find of the word Hello gives 0, because Hello starts at the very first position.

Here's where that bites. A program wants to report whether s holds a z. It writes, if s dot find of z, then print "found a z," otherwise print "no z." There's no z anywhere in Hello, World, yet the run displays "found a z." Here's why. Find handed back minus 1, and the if line asked whether minus 1 counts as true. Only the number 0 counts as false, so minus 1 counts as true, and the wrong branch ran.

The fix is to ask the right question: is the position not equal to minus 1? With that test, the run displays "no z." The other cure is the in test you met with lists, which also works on strings. The test z in s hands back a real Boolean, False.

Pause and try this. Find of Hello on the string Hello, World gives 0. If a program wrote "if s dot find of Hello," which branch would run?

Remember which number counts as false.

Here's the answer. The else branch would run, even though Hello is there. Find handed back 0, and 0 counts as false, so the test failed on a real match.

Find answers where, not whether; minus 1 means nowhere.

6. Replace and count every match

If you've ever used find-and-replace in a word processor, you've seen it change every match in the document at once, not only the first one. String methods behave the same way.

Replace is the string method that takes an old piece of text and a new one and hands back a new string with every match of the old text swapped for the new one. Count is the string method that hands back how many times the text it is given appears in the string.

Here's a to-do note: buy milk, buy eggs, buy bread. Count of buy gives 3. Replace, swapping buy for get, displays get milk, get eggs, get bread, with all three changed. Then the program prints the note itself, and it still says buy, buy, buy, because replace built a new string and nothing stored it. That's the reassign lesson from earlier, back again.

The pinned method set shows the same rule with letters: replacing each small o with a zero displayed Hell0, W0rld, with both o's changed.

Pause and try this. Count of buy on the to-do note gives 3. How many words does replace change in the new string?

Think about what count told you.

Here's the answer. All three. Replace swaps every match, so the number of changes equals the count.

Replace changes every match, but only in the copy.

7. Build small text algorithms

A chef with a knife, a peeler, and a pan can make many dishes. You now have nine string methods, slices, loops, and the counting idiom. Put them together and you get small text programs you can reuse again and again.

To count words, split and measure the list. The group-chat message "meet me at the bike rack at noon" splits into 8 words. Count of at gives 2.

To compare typed replies fairly, normalize first. To normalize is to put text into one standard form before comparing it, such as trimming the spaces at both ends and turning every letter small, so that a reply typed as Yes matches yes. The reply, typed with two spaces in front, a capital Y, and a space after, compared with yes, gives False. Strip is the string method that hands back a new string with the whitespace removed from both ends. Chain strip and then lower, and the result is yes. Now the comparison gives True.

To reverse a string, use the slice with a step of minus 1. The pinned reverse of hello is olleh. No loop needed. The word level reversed is still level, so the check gives True, and river reversed is revir.

To count only certain lines, loop and test with startswith, the string method that hands back True when the string begins with the text it is given. Three log lines, two starting with day and one starting with note, give a count of 2.

And to tally words, split a line and feed the words to the counting idiom from Week 10. The line red blue red red blue gives red 3 and blue 2.

Pause and try this. A reply arrives with a capital Y and spaces around it. Which two methods, in which order, make it match yes?

Think about the ends first, then the letters.

Here's the answer. Strip, then lower. Strip removes the spaces at both ends, and lower turns the capital Y small, so the result equals yes.

Split to count; strip and lower to compare; step back to reverse.

8. Check a chatbot's claim with a run

A friend tells you the sign on a store says it opens at nine. You can trust the friend, or you can walk past and read the sign. A chatbot's explanation of code is the friend's report, and the run is the sign itself.

Suppose you ask a chatbot what the pinned program prints, the one where s gets Hello and upper sits on a line by itself. It answers that upper converts s to capitals, so the program prints HELLO. You run it. The output is Hello. The chatbot described upper correctly, but it treated it as if it changed the string in place, which is the exact bug from Section 2.

A chatbot predicts likely text; it doesn't run your program. For the official word, the Standard Library reference's String Methods section on docs.python.org is where these methods are written down. One honest limit of this lecture: reading and writing files comes later, and regular expressions belong to a later course.

Pause and try this. The chatbot's description of what upper does was accurate. So why was its prediction wrong?

Separate what the method does from what the program prints.

Here's the answer. Upper does build a capitalized string, but the program never stored it, so s stayed Hello. The prediction was about the program, and only the run can settle that.

Trust the run, not the explanation.

Closing

Let me pull it together in the order we built it. A string method is called with a dot and hands back a new value, so the pinned set ended with Hello, World unchanged. A method on a line by itself changes nothing, and s equals s dot upper is the cure. Split makes a list and join makes a string, so day 3: 25 pages became four pieces and came back as day 3: 30 pages. Split with empty parentheses cuts on whitespace, not commas. Find hands back a position or minus 1, never True or False. Replace and count work on every match. Split, strip, lower, and a backward slice build the small text programs. And every claim about text gets checked by a run.

Next in this module come the slide deck, the Readings and Resources page, the lecture tutorial, the practice set, the quiz, and the Coding Lab. Then, in Week 12, on files and exceptions, you'll open files and read them line by line, and every line you load will need the split and strip you learned here.


Built with The Course Maker · samples.thecoursemaker.com