Real text is messy: capital letters, extra spaces, punctuation, lines that arrive in a strange form. Most programs spend much of their time reading, cleaning and cutting text. In this course you cut text with split, glue it with join, clean it with strip, lower and replace, count the words with a dictionary, write and break a secret code, and check palindromes and anagrams. A lesson on looking inside text shows how to find a piece, test the kind of characters, and read a line such as name equals value. A text toolkit with a table of tests is the project at the end. The practice runs your functions on messy inputs and tells you which ones fail.
5 lessons, about 2 h 30 min5 simulations13 problems10 practice questionsPython in every lesson
split cuts a text into a list of pieces. With no argument it cuts at every run of spaces and drops the extra ones, and with a separator it cuts at that piece only and keeps empty pieces. join glues a list into one text, and the separator comes first. strip removes the spaces at the two ends, replace changes every old piece into a new piece, and startswith, endswith and in answer True or False. A string cannot change, so every method gives a new string, and the result must be kept in a variable.
Real text is messy: The, the and THE are three different strings, and a word with a full stop is not the same as the word without. Before counting, clean the text in the same steps: make every letter small with lower, remove the punctuation with replace, cut the text into words with split, and then count with a dictionary or ask a question about the list. The key of the dictionary is the word and the value is how many times it was seen. max with key equals len gives the longest word, and max with key equals counts.get gives the most common word. A list comprehension can pick out the letters that pass a test.
Inside the computer every letter is a number. ord gives the number of a letter and chr gives the letter back, and the letters of the alphabet follow each other, so ord of a letter minus ord of a is its place counting from 0. A Caesar cipher moves every letter a few places along the alphabet, and the percent 26 operation makes the letters at the end wrap round to the start. Decoding moves them back by the same amount. A palindrome reads the same backwards and two anagrams use the same letters, and both are tested after cleaning the text. An f-string can format numbers and align columns.
To look inside a text, find gives the position of the first match and minus one when there is none, rfind gives the last match, index does the same but raises a ValueError, and count says how many times. Tests such as isdigit, isalpha, isalnum, isspace, isupper and islower ask what kind of characters a text has. partition cuts a text at the first separator into three parts, which makes a line of the form key equals value easy to read, with a check for a missing separator or an empty key. Always check a line before you trust it, and turn the text of a number into a number only when it has digits.
In this project we build a text toolkit: small functions for messy text that each do one job, and a table of tests that runs them all and shows PASS or FAIL. The functions give the tidy words of a text, count the words, find the most common word with a rule for ties, write a name in title case, and shift a text for a Caesar code. We plan the expected answers by hand, build the functions from the tools of the course, run the table of tests, and extend the toolkit with a palindrome test, a word frequency report and a code breaker.
Log in to join the discussion.
Loading…