Python program to counting consonants in a given word example

11/25/2023

python program to count consonants in a given word with example and output

Go Back

Python Program to Count Consonants in a Word (With Example & Output)

In Python, working with strings is a common task in both beginner and advanced programming. One frequently asked problem is how to count the number of consonants in a given word or string. This type of problem helps in understanding string manipulation, loops, and conditional logic in Python. A consonant is any alphabet that is not a vowel (a, e, i, o, u). By writing a Python program to count consonants, you can improve your skills in handling character-based operations and building simple algorithms. In this tutorial, we will learn how to write a Python program to count consonants in a given word, along with a clear example and output. This guide is especially useful for beginners, students, and those preparing for coding interviews. vowel = ['a', 'e', 'i', 'o', 'u'] word = "Developer Indian programming" count = 0 for character in word: if character not in vowel: count += 1 print(count)

Introduction

In Python, working with strings is one of the most common tasks for beginners and developers alike. A popular programming problem is to count the number of consonants in a given word or string. This helps in understanding loops, conditions, and string manipulation in Python.

A consonant is any alphabet character that is not a vowel (a, e, i, o, u). In this article, you will learn how to write a Python program to count consonants, along with examples, logic, and output.


python program to count consonants in a given word with example and output

What is a Consonant?

A consonant is any letter in the alphabet except:
a, e, i, o, u

For example:

  • Word: Python

  • Consonants: P, y, t, h, n → Total = 5


Python Program to Count Consonants

word = input("Enter a word: ")

count = 0

for char in word:
    if char.isalpha() and char.lower() not in "aeiou":
        count += 1

print("Number of consonants:", count)

Example Output

Enter a word: Developer
Number of consonants: 6


Explanation of Code

  • input() → Takes user input

  • for char in word → Iterates through each character

  • isalpha() → Ensures only letters are counted

  • char.lower() → Handles uppercase and lowercase

  • not in "aeiou" → Excludes vowels

  • count += 1 → Increments consonant count


Using List Comprehension (Short Method)

word = input("Enter a word: ")

count = sum(1 for char in word if char.isalpha() and char.lower() not in "aeiou")

print("Number of consonants:", count)

Time Complexity

Time Complexity: O(n)


Common Mistakes

  • Not converting characters to lowercase

  • Counting spaces or special characters

  • Including vowels accidentally


Real-World Use Cases

  • Text processing applications

  • Input validation

  • NLP basics

  • Coding interview questions


Conclusion

Counting consonants in a string is a simple yet powerful way to understand string manipulation in Python. By using loops, conditions, and built-in functions, you can easily solve this problem and build a strong foundation in programming.
The idea behind the Python programme to count the vowels or consonants in a given string will be covered in this article.

This article will teach you how to use the Python programming language to count the vowels and consonants in a given string.

Frequently Asked Questions (FAQ)

1. How do you count consonants in a string using Python?

You can count consonants by looping through each character in the string and checking if it is an alphabet but not a vowel. Use conditions to exclude vowels (a, e, i, o, u).


2. What is a consonant in Python string processing?

A consonant is any alphabet character that is not a vowel. In Python, you can check this using conditional statements like if char.isalpha() and char not in "aeiou".


3. How to count both vowels and consonants in Python?

You can use a loop to check each character. If it is a vowel, increment a vowel counter; otherwise, if it is an alphabet, increment a consonant counter.


4. How to ignore spaces and special characters while counting consonants?

Use the isalpha() method in Python to ensure only alphabet characters are considered, ignoring spaces, numbers, and symbols.


5. Can we count consonants using list comprehension in Python?

Yes, you can use list comprehension for a shorter solution:

count = sum(1 for char in word if char.isalpha() and char.lower() not in "aeiou")

6. How to make the consonant count case-insensitive in Python?

Convert the string to lowercase using lower() before checking conditions. This ensures both uppercase and lowercase letters are handled correctly.


7. What is the time complexity of counting consonants in a string?

The time complexity is O(n), where n is the length of the string, since each character is checked once.


8. What are common mistakes while counting consonants in Python?

  • Not converting characters to lowercase

  • Including spaces or special characters

  • Forgetting to exclude vowels

 

Table of content