0%

Working with Text · capstone

Capstone project: Project: Build an Interactive Text Inspector

You can now treat a as structured data instead of an indivisible message. This project brings the chapter together in an interactive text inspector.

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 , which come in later chapters.

Process

Create these six values from original:

Required
cleanedoriginal with surrounding whitespace removed
character_countthe length of cleaned
first_characterthe first character of cleaned
last_characterthe last character of cleaned
uppercasean uppercase version of cleaned
lowercasea 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 return new values. They do not change cleaned.

4. Present

Remove temporary checks and print the complete report with .

Before you submit

Check that:

  • original contains the value returned by input();

  • cleaned comes 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, sequences. That model will make easier to understand when you reach Chapter 6, and the differences between the two will matter just as much.

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.

  1. Store the input in original and strip it into cleaned.

  2. Calculate character_count from cleaned.

  3. Retrieve first_character and last_character from cleaned.

  4. Create uppercase and lowercase versions.

  5. Print the required eight-line report.

Use the taught operations and print the report with .

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.