Python program to counting consonants in a given word example
python program to count consonants in a given word with example and 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)
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.
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
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)
Enter a word: Developer
Number of consonants: 6
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
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: O(n)
Not converting characters to lowercase
Counting spaces or special characters
Including vowels accidentally
Text processing applications
Input validation
NLP basics
Coding interview questions
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.
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).
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".
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.
Use the isalpha() method in Python to ensure only alphabet characters are considered, ignoring spaces, numbers, and symbols.
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")
Convert the string to lowercase using lower() before checking conditions. This ensures both uppercase and lowercase letters are handled correctly.
The time complexity is O(n), where n is the length of the string, since each character is checked once.
Not converting characters to lowercase
Including spaces or special characters
Forgetting to exclude vowels