Count Vowels in a String

Easy ⏱ 8 min 83% acceptance ★★★★☆ 4.3
Write a function count_vowels(text) that counts how many characters in text are vowels (a, e, i, o, u), case-insensitive, using a for loop over the characters.

Examples

Example 1
Input
count_vowels("Hello World")
Output
3
Explanation

e, o, o are vowels.

Example 2
Input
count_vowels("xyz")
Output
0
Explanation

No vowels present.

Constraints

  • text is a string (may include spaces and punctuation).

Topics

Loopsfor loop

Companies

AmazonFlipkart

Hints

Hint 1

Lowercase each character before checking membership in "aeiou".

Hint 2

Use a counter variable incremented inside the loop.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.