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

Count Vowels

Count English vowel characters in text with Python. Students traverse a string, test membership in both cases, and count occurrences not distinct letters.

BeginnerPhase 5 - Category 1: Basic String HandlingExample 5 of 10
count-vowels.py
1# Take string input
2s = input("Enter a string: ")
3
4# Count vowels
5vowels = "aeiouAEIOU"
6count = 0
7for char in s:
8 if char in vowels:
9 count += 1
10
11print(f"Number of vowels: {count}")

Output

Enter a string: Hello
Number of vowels: 2

In this one

  • Test vowel membership
  • Count occurrences
  • Handle both English cases

How it works

  1. 1

    Define the listed English vowels and initialize count.

  2. 2

    Visit each source character.

  3. 3

    Increment only for membership matches, then print.

Edge cases

Does uppercase E count?

Does uppercase E count?

Yes. E is listed in vowels.

Does y count?

Does y count?

No. It is absent from this vowel set.

What's going on

The vowel string lists a, e, i, o, u in lowercase and uppercase. Each source character is tested against it.

Every match adds one. For Banana, the three occurrences of a give a count of three, not one distinct vowel. Spaces and digits fail the membership test. Accented vowels and y are not included in this explicitly defined English set.

Does uppercase E count? Yes. E is listed in vowels.

Does y count? No. It is absent from this vowel set.

Quiz

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

Question 1

What does the counter measure?

Pick an answer to check it.

Question 2

Does uppercase E count?

Pick an answer to check it.

Questions people ask

Does uppercase E count?

Yes. E is listed in vowels.

Does y count?

No. It is absent from this vowel set.

What does the counter measure?

Vowel occurrences.