Count Consonants in String

In this lesson, you will learn how to count consonants of a string using both an iterative and recursive approach in Python.

We'll cover the following

In this lesson, we focus on the following problem:

Given a string, calculate the number of consonants present.

The vowels are the following letters:

a e i o u

Any other letter that is not a vowel is a consonant.

Let’s have a look at an example:

Welcome to Educative!

has 9 consonants.

Before you dive into the implementation, consider the edge cases such as spaces and exclamation marks. This implies that if a character is not a vowel, it has to be a letter to be considered a consonant.

Iterative Approach

Check out the iterative approach in the snippet below:

Get hands-on with 1400+ tech skills courses.