textwrap and the string module
Real text is messy: stray spaces, mixed case, punctuation, odd line endings. This topic puts the whole phase together: split text into lines and words, clean it, count words, wrap it with textwrap, use the string constants, and parse a small log file.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Count the vowels in "Education is the key".
Find the longest word in "the quick brown foxes jumped" with a loop and two variables.
Wrap "one two three four five six seven" to lines of at most 15 characters.
Explain it without notes
Why should you clean text before counting or comparing it?
What is the difference between splitlines() and split("\n")?
How does line.split(None, 3) differ from line.split(), and why is it useful for logs?
When would you use partition instead of split to read key=value text?
Expected output
Python is a programming
language that lets you work
quickly and integrate systems
effectively.
Python is a programming language that...
> a
> b
'\nindented\n more\n'
abcdefghijklmnopqrstuvwxyz
ABCDE 0123456789
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
True