Count Consonants in String

Count the number of consonants (alphabetic non-vowels) in a string.

BeginnerTopic: String Programs
Back

Python Count Consonants in String Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to count consonants in a string

s = input("Enter a string: ")

vowels = "aeiouAEIOU"
count = sum(1 for ch in s if ch.isalpha() and ch not in vowels)

print("Number of consonants:", count)
Output
Enter a string: hello world
Number of consonants: 7

Understanding Count Consonants in String

We count characters that are alphabetic but not vowels.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents