Zed-King Institute

    Python Program to Count Vowels in a String

    mediumStringsPython

    A for loop can walk through a string one character at a time — here it counts the vowels.

    The question

    Read a sentence and print how many vowels (a, e, i, o, u — upper or lower case) it contains.

    The code

    program.py

    s = input()
    count = 0
    for ch in s.lower():
        if ch in "aeiou":
            count += 1
    print("Vowels =", count)

    Input

    Things Module Arduino Pao of

    Output

    Vowels = 11

    How it works

    1. 1s.lower() makes capital vowels count too.
    2. 2for ch in ... visits every character, including spaces.
    3. 3Whenever ch is in "aeiou", add 1 to count.

    Common mistakes

    • Forgetting capital letters — "Internet" then has one vowel too few.
    • Resetting count = 0 inside the loop.

    Now solve a similar question yourself

    A new Python question every time, checked instantly.

    Practice questions

    More in Strings