Count String Consonants in Python

  • Post category:String

In this article, you will see how to count string consonants in Python. A consonant is a letter that is pronounced with a consonant sound. In English, the consonants are all letters except a, e, i, o, u. In Python, to count the consonants in a string, you can use the following code.

Step 1: Create a string

str = "CodeAllow"
print("str = ", str)
print("str type = ", type(str))

Step 2: Count string consonants

print("Consonants = ", sum(str.count(consonant) for consonant in "bcdfghjklmnpqrstvwxyz"))

Output:

str =  CodeAllow
str type =  <class 'str'>
Consonants =  4

Free resources to learn advanced skills: AiHints and CodeAllow