Python Practice › File Handling Programs › Program 35
Count consonants in a file
Write a Python program to count the consonants in a file.
Output
16
Approach
- Letters that are not vowels.
Solution
with open("story.txt", "w") as f: f.write("Python file handling is easy\n") with open("story.txt") as f: text = f.read().lower()print(sum(ch.isalpha() and ch not in "aeiou" for ch in text))Output
16
Learn the concept
New to this topic? Read the lesson, then come back.