Count Consonants

Count number of consonants in string.

BeginnerPhase 5 - Category 1: Basic String HandlingExample 6 of 10
count-consonants.py
1# Take string input
2s = input("Enter a string: ")
3
4# Count consonants
5vowels = "aeiouAEIOU"
6count = 0
7for char in s:
8 if char.isalpha() and char not in vowels:
9 count += 1
10
11print(f"Number of consonants: {count}")

Output

Enter a string: Hello
Number of consonants: 3

What's going on

Count alphabets that are not vowels.

Key Concepts:

Check if character is alphabet
Check if not a vowel
Count consonants