← All lessons

Mini-project: Text Analyzer

Sign in · Sign up

Lesson 4.11

A text analyzer can be built from string methods:

text = input("Enter some text: ").strip().lower()

words = text.split()
vowels = 0

for letter in text:
    if letter in "aeiou":
        vowels += 1

Look at the chain on the first line: strip() removes spaces at the edges, lower() turns the text into lowercase — so that uppercase vowels are counted too.

In this check the text is provided by the test, so you do not need input(): the ready text is already in the variable text.

Print three lines:

  1. Words: <number of words>;
  2. Characters: <length of the text>;
  3. Vowels: <number of vowels>.

Count the words with split, the characters with len, the vowels with a loop and the condition letter in "aeiou".

Hint

A method chain is a series of calls joined by dots.

strip removes spaces at the edges, lower makes everything lowercase — so uppercase vowels are counted too. Then split for the words and a loop for the vowels.