2. Read a text file and display the number of vowels/consonants/uppercase/lowercase characters in the file.


defcount_characters(filename):

vowels = "aeiouAEIOU"

vowels_count = 0

consonants_count = 0

uppercase_count = 0

lowercase_count = 0

 

with open(filename, 'r') as file:

for line in file:

for char in line:

ifchar.isalpha():

ifchar.lower() in vowels:

vowels_count += 1

else:

consonants_count += 1

 

ifchar.isupper():

uppercase_count += 1

elifchar.islower():

lowercase_count += 1

 

print("Vowels:", vowels_count)

print("Consonants:", consonants_count)

print("Uppercase:", uppercase_count)

print("Lowercase:", lowercase_count)

 

if __name__ == "__main__":

filename = "input.txt"  # Replace with your file name

count_characters(filename)

No comments:

Post a Comment