Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Count Words Starting with Vowel

Count words beginning with English vowels using Python. Students split whitespace and test the first character, including uppercase and punctuation cases.

IntermediatePhase 5 - Category 3: Word OperationsExample 8 of 10
count-words-starting-with-vowel.py
1# Take string input
2s = input("Enter a string: ")
3
4# Count words starting with vowel
5words = s.split()
6vowels = "aeiouAEIOU"
7count = 0
8for word in words:
9 if len(word) > 0 and word[0] in vowels:
10 count += 1
11
12print(f"Words starting with vowel: {count}")

Output

Enter a string: Apple Orange Banana
Words starting with vowel: 2

In this one

  • Inspect a token's first character
  • Handle both vowel cases
  • Retain punctuation effects

How it works

  1. 1

    Split into words.

  2. 2

    Check each nonempty word's first character.

  3. 3

    Increment when that character is a listed vowel.

Edge cases

Does Apple qualify?

Does Apple qualify?

Yes. Uppercase A is listed.

Does a token starting with a quote qualify?

Does a token starting with a quote qualify?

No. The first character itself must be a listed vowel.

What's going on

Each token is checked for a nonempty first character in a vowel string containing both cases. A qualifying token increments count.

Apple and orange count, but a token beginning with a quotation mark does not, even if its next character is a vowel. split already produces nonempty tokens; the explicit length guard makes the indexing assumption visible. Entire-word vowel counts are not needed.

Does Apple qualify? Yes. Uppercase A is listed.

Does a token starting with a quote qualify? No. The first character itself must be a listed vowel.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is inspected per token?

Pick an answer to check it.

Question 2

Does Apple qualify?

Pick an answer to check it.

Questions people ask

Does Apple qualify?

Yes. Uppercase A is listed.

Does a token starting with a quote qualify?

No. The first character itself must be a listed vowel.

What is inspected per token?

word[0].