Table of Contents
Python program to read data from file and count characters, digits, spaces, etc.
Write a function that reads the content of file poem.txt and counts the number of alphabets, blank spaces, lowercase character and uppercase character, and number of occurrences of word “beautiful” in the file in Python.
Python program to read data from file and count characters-digits-spaces-lower-case-upper-case
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | def read_and_count(): lower_case=0 upper_case=0 alphabet=0 space=0 digit=0 string_count=0 with open("poem.txt") as iv: c=iv.readlines() for j in c: for i in range(len(j)): if(j[i].isalpha()): alphabet+=1 if(j[i].isdigit()): digit+=1 if(j[i].islower()): lower_case+=1 if(j[i].isupper()): upper_case+=1 if(j[i] == " "): space+=1 string_count+=j.lower().count("beautiful") print("Alphabets : ",alphabet) print("Space : ",space) print("Digits : ",digit) print("Lower Case : ",lower_case) print("Upper Case : ",upper_case) print("Count of 'beautiful' : ",string_count) read_and_count() |
Output

We are aiming to explain all concepts of python in easiest terms as possible.

ITVoyagers
Author
PRACTICALS/PRACTICE PROGRAM IN PYTHON
CHECKOUT OTHER RELATED TOPICS
CHECKOUT OTHER QUIZZES
Quizzes on File Handling |
Easy quiz on file handling in python part 1 |
Easy quiz on file handling in python part 2 |
Quizzes on Exception Handling |
Easy quiz on Exception Handling in python part 1 |
Easy Quiz on Exception Handling in python part 2 |
Quizzes on Regular Expressions |
Easy Quiz on Regular Expression in python part 1 |
Quizzes on Python concepts coming soon… |
Python basic quiz |
New and easy python quiz basic part 1 |
New and easy python quiz (basic) part 2 |
New and easy python quiz (basic) part 3 |
Python practice quiz set |
Best Practice Quiz Set on Python Programming Part 1 |
Best Practice Quiz Set on Python Programming Part 2 |
Best Practice Quiz Set on Python Programming Part 3 |
Best Practice Quiz Set on Python Programming Part 4 |
Best Practice Quiz Set on Python Programming Part 5 |