Count consonants in a file

EasyFile Handling Programs · 35 of 38

Write a Python program to count the consonants in a file.

Advertisement
Output
16

Approach

  1. Letters that are not vowels.

Solution

Python
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
Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More file handling programs

← All 38 File Handling Programs