Start learning free

LESSON 216 OF 287 · LOOPS WITH STRINGS, LISTS & DICTIONARIES

Count the Vowels in Python: Looping Over a String

Practice Read the full explanation

Given a string s, return how many vowels it has.

The vowels are a, e, i, o and u, in lower case or in upper case. The letter y is not a vowel here. Every other character (consonants, spaces, digits, punctuation) is skipped.

 

Example 1:

Input: s = "Hello World"
Output: 3
Explanation: The vowels are e, o and o. The other characters, the space included, are skipped.

Example 2:

Input: s = "AlgoCademy"
Output: 4
Explanation: A, o, a and e are vowels: the upper-case A counts too. The y doesn't count.

Example 3:

Input: s = "Why try?"
Output: 0
Explanation: y is not a vowel, so this string has none.

 

Constraints:

  • s has at least one character and doesn't start or end with a space.
  • s contains English letters, digits, spaces and punctuation.
STEP 1 OF 5
    Ctrl + Enter to check

    PYTHON · LOOPS WITH STRINGS, LISTS & DICTIONARIES

    Count The Vowels in Python, explained

    Problem overview

    The function countVowels receives a string s and returns how many vowels it has. The vowels are a, e, i, o and u, in lower or upper case. The letter y is not a vowel here, and every other character, such as consonants, spaces, digits and punctuation, is skipped.

    In "Hello World" the vowels are e, o and o, so the answer is 3. In "AlgoCademy" they are A, o, a and e, which makes 4: the capital A counts and the y doesn't. In "Why try?" there are none, so the answer is 0.

    Approach

    Look at each character in turn and decide whether it is a vowel. If it is, add one to a counter. To decide, keep all ten vowels, both cases, in one string, "aeiouAEIOU", and ask whether the character appears in it. Listing both cases avoids converting anything to lower case.

    Python lets a for loop walk over a string directly, giving one character at a time, so no index is needed.

    Step by step

    1. Store the vowels in a string named vowels: "aeiouAEIOU". The letter y isn't in it.
    2. Start count at 0. Nothing has been checked yet.
    3. Loop over s with a variable c. Each turn, c is the next character.
    4. If c in vowels, add 1 to count. The operator in works on strings: it is True when the text on its left appears inside the text on its right.
    5. Return count after the loop, so that every character has been checked first.

    The solution in Python

    class Solution:
        def countVowels(self, s: str) -> int:
            vowels = "aeiouAEIOU"
            count = 0
            for c in s:
                if c in vowels:
                    count += 1
            return count
    

    Details worth knowing:

    • in on a string looks for a substring. "e" in "aeiou" is True and "E" in "aeiou" is False, because the comparison is case-sensitive. That is why both cases are listed. Since c is always a single character here, there is no surprise, but "ae" in "aeiou" is True too, and so is the empty string "" in "aeiou".
    • Looping over a string. for c in s yields each character as a string of length 1. Spaces, digits and punctuation are characters like any other. They are just not in vowels, so they are skipped.
    • An alternative with lower(). sum(1 for c in s if c.lower() in "aeiou") also gives 3 for "Hello World". The lesson's version spells out the loop.
    • count is just a variable. Strings also have a method called count, but the name here belongs to the local variable. It doesn't clash with anything.

    Edge cases and common mistakes

    • Upper-case vowels. Checking only "aeiou" would count "AlgoCademy" as 3 instead of 4.
    • The letter y. "Why try?" returns 0, since y is not in the vowel string.
    • Returning inside the loop. A return indented under the if ends the function at the first vowel, so the answer could never be more than 1.
    • Counting everything. Putting count += 1 outside the if counts every character, not only the vowels.
    • Non-letters. A string like "rhythm 123" returns 0. Digits and spaces are skipped without any special handling.

    Testing

    • "Hello World" gives 3.
    • "AlgoCademy" gives 4.
    • "Why try?" gives 0.
    • "AEIOU" gives 5.
    • "rhythm 123" gives 0.
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free