Project 2 (Text Analyzer)

Build a small program that analyzes a sentence using strings, loops, and string methods.

Loading...
Project 2 (Text Analyzer)

The Task

Build a program that:

  • Asks the user to enter a sentence.
  • Prints the length of the sentence.
  • Prints how many words it contains.
  • Prints the sentence in uppercase and lowercase.
  • Counts how many vowels (a, e, i, o, u) it contains.
  • Asks for a word and checks whether the sentence starts with it.

What You’ll Use

  • input() to take the sentence and the word
  • len() to find the length
  • split() to count words
  • upper() and lower() to change the case
  • count() to count vowels
  • A for loop to check each vowel
  • startswith() to check the beginning of the sentence

Expected Output

Enter a sentence: Python is fun to learn
Enter a word to check: Python
 
--- Text Analysis ---
Length: 22
Word count: 5
Uppercase: PYTHON IS FUN TO LEARN
Lowercase: python is fun to learn
Vowel count: 6
Starts with 'Python': True

Try It Yourself First

Here are a few hints:

  • split() splits the sentence into words. len() on the resulting list gives the word count.
  • You can count each vowel using count() and add the results together.
  • Convert the sentence to lowercase before counting vowels so both "A" and "a" are counted.
  • startswith() returns True or False, so you can print its result directly.

Give it a try before you scroll down.

Solution

# Text Analyzer
 
sentence = input("Enter a sentence: ")
word = input("Enter a word to check: ")
 
length = len(sentence)
 
words = sentence.split()
word_count = len(words)
 
vowels = "aeiou"
lower_sentence = sentence.lower()
 
vowel_count = 0
 
for letter in vowels:
    vowel_count = vowel_count + lower_sentence.count(letter)
 
print("\n--- Text Analysis ---")
print("Length:", length)
print("Word count:", word_count)
print("Uppercase:", sentence.upper())
print("Lowercase:", sentence.lower())
print("Vowel count:", vowel_count)
print("Starts with", word, sentence.startswith(word))

How It Works

  • len(sentence) gives us the number of characters in the sentence, including spaces. We store the result in the length variable.
  • sentence.split() breaks the sentence into a list of words. We store that list in the words variable.
  • len(words) then tells us how many words are in the list.
  • The vowels variable stores the string "aeiou".
  • lower_sentence stores a lowercase version of the sentence so that both uppercase and lowercase vowels can be counted.
  • We start vowel_count at 0. We’ll add to it as we count each vowel.
  • The for loop goes through each vowel in "aeiou".
  • count() tells us how many times the current vowel appears in the sentence.
  • We add each count to vowel_count to get the total number of vowels.
  • startswith(word) checks whether the sentence starts with the word entered by the user.

One Thing To Remember

If you count vowels without converting the sentence to lowercase first:

vowel_count = 0
 
for letter in vowels:
    vowel_count = vowel_count + sentence.count(letter)

this will miss uppercase vowels.

For example, "Apple" contains an uppercase "A", but:

"Apple".count("a")

returns 0.

count() is case-sensitive.

That’s why we use:

lower_sentence = sentence.lower()

before counting.

Challenges

If you want to go a little further. Try these:

  • Check whether the sentence ends with a question mark using endswith().
  • Replace a word in the sentence using replace().

👉 Next tutorial: Python Operators