Working with Text · capstone
Capstone project: Project: Build an Interactive Text Inspector
You can now treat a
The program will ask for a non-empty phrase, clean its surrounding whitespace, inspect several properties, and print a report.
Input
Ask one question and store the returned string in original:
original = input("Enter a non-empty phrase: ")
The phrase may contain spaces at the beginning or end. Keep the original answer so the report can show what was entered.
This project deliberately requires at least one character that is not whitespace, such as a letter, digit, or punctuation mark. Checking and recovering from an empty answer needs decisions and
Process
Create these six values from original:
| Required | |
|---|---|
cleaned | original with surrounding whitespace removed |
character_count | the length of cleaned |
first_character | the first character of cleaned |
last_character | the last character of cleaned |
uppercase | an uppercase version of cleaned |
lowercase | a lowercase version of cleaned |
Use the string operations taught in this chapter. Do not type results that only work for one particular phrase.
Output
Print an eight-line report in this form:
TEXT INSPECTOR
Original: [ Python Land ]
Cleaned: Python Land
Characters: 11
First character: P
Last character: d
Uppercase: PYTHON LAND
Lowercase: python land
The brackets on the Original line are part of the report. They make surrounding spaces visible. Do not add brackets to the Cleaned line.
Build in checkpoints
1. Collect and clean
Run the program with spaces around a short phrase. Temporarily print original and cleaned inside brackets so you can see the difference.
2. Measure and index
Calculate the character count. Retrieve the first and last characters from cleaned, not from the uncleaned input.
3. Transform
Create the uppercase and lowercase strings. Remember that the cleaned.
4. Present
Remove temporary checks and print the complete report with
Before you submit
Check that:
originalcontains the value returned byinput();cleanedcomes from.strip();the length and indexed characters come from
cleaned;uppercase and lowercase are stored separately;
the report has the required eight non-empty lines after the prompt;
the Original line shows its value inside square brackets.
If your plan includes Guided, you can ask Monty:
Review my text inspector in Input, Process, Output order. Check whether every derived value comes from the correct string, and point out one issue at a time.
After it passes
Run the program with punctuation, mixed capitalization, or several spaces between words. Notice what .strip() changes and what it leaves alone.
You now have a precise model of strings as ordered,
Notice what your inspector cannot do. It prints the same eight kinds of information for every suitable phrase. It cannot choose an extra message for a long phrase or turn away an empty answer. The next chapter introduces decisions that let a program choose which instructions to run.
Task
Build an interactive text inspector.
Store the input in
originaland strip it intocleaned.Calculate
character_countfromcleaned.Retrieve
first_characterandlast_characterfromcleaned.Create
uppercaseandlowercaseversions.Print the required eight-line report.
Use the taught
Enter a phrase containing at least one non-whitespace character. Surrounding whitespace is allowed; checking and recovering from an unsuitable answer comes in later chapters.