HOWTO · Python

How to Count Syllables in Python

Learn four Python approaches for counting vowel letters as a rough syllable estimate.

On this page

Python can count vowel letters in a word or sentence, but that count is only a rough estimate of syllables. The examples below show four ways to count the letters a, e, i, o, and u, including how to normalize capitalization and inspect each vowel’s count. One approach uses set() to store the distinct vowels for membership checks. These examples do not determine how many syllables a person speaks.

Count vowel letters with a loop and if statement

A loop checks each character, and an if statement adds one when that character belongs to the vowel string. Calling casefold() first lets the same membership check handle uppercase and lowercase English vowels. This is the most direct option when you need one total.

Run this example:

word = "Beautiful"
vowels = "aeiou"
count = 0

for letter in word.casefold():
    if letter in vowels:
        count += 1

print(count)
5

The result is five matching vowel letters. The repeated u is counted twice because this loop counts occurrences, not distinct letters.

Count vowels with a dictionary comprehension

Use a dictionary when you want a separate occurrence count for every vowel. The comprehension iterates over the vowels to create the keys, then sums matching characters in the normalized word for each value. The variable in the comparison must be the character from the word; comparing against the key produces the intended per-vowel counts.

word = "Beautiful".casefold()
vowel_counts = {
    vowel: sum(1 for letter in word if letter == vowel)
    for vowel in "aeiou"
}
print(vowel_counts)
{'a': 1, 'e': 1, 'i': 1, 'o': 0, 'u': 2}

This dictionary reports letter frequencies, not syllable counts. A vowel with no matches remains present with a value of zero.

Count vowels with dict.fromkeys()

dict.fromkeys() initializes one key per vowel with the same starting value. Here each value starts at zero; the loop then increments the entry for each matching character. This approach is useful when you want to see the initialization and accumulation steps separately.

word = "Beautiful".casefold()
vowel_counts = dict.fromkeys("aeiou", 0)

for letter in word:
    if letter in vowel_counts:
        vowel_counts[letter] += 1

print(vowel_counts)
{'a': 1, 'e': 1, 'i': 1, 'o': 0, 'u': 2}

The dictionary values are integers, so sharing the initial value is safe here. The keys are the five lowercase vowel letters, and the iteration order follows the supplied string.

Count vowel letters with a set and function

A set provides membership checks for a collection of distinct vowels. Put the logic in a function to reuse it with other words. The function returns a count, leaving the caller to decide whether to print it or combine it with other processing.

def count_vowel_letters(word):
    vowels = set("aeiou")
    return sum(1 for letter in word.casefold() if letter in vowels)


print(count_vowel_letters("Beautiful"))
5

Although the set contains each vowel only once, every matching occurrence in the word contributes one to the total. This version also folds uppercase English letters before checking membership.

Understand what vowel counting does and does not count

These examples count occurrences of the five basic English vowel letters. They do not split words into spoken syllables: spelling and pronunciation do not map one-to-one, and a vowel letter may be silent or combine with another letter to represent one sound. For example, the code finds five vowel-letter matches in Beautiful, while the word has three spoken syllables. This difference shows why counting vowel letters does not reliably count syllables.

The examples also use a deliberately small alphabet. Accented vowels, y, and language-specific spelling rules are not included, so adapt the set of characters to the text you process. casefold() handles case normalization; it does not remove accents or infer pronunciation. For a quick rough estimate on English text, these patterns are simple and transparent. For dependable spoken-syllable counts, vowel-letter counting alone is not enough.