Python program to count number of uppercase, lowercase, space in a text file

 Python Program to count number of uppercase, lowercase, space in a text file



Q:- Write a python function Counting() to count uppercase, lowercase, digits, spaces and special characters present in text file "story.txt".

Logic and Explanation

In this question, we have to go through all the characters of a text file and count the number of uppercase, lowercase, spaces, special characters and digits present in it. First, we will create variables of counting for all the possible cases given in the question in which we will add and count. Then, we will open our text file in our python file by using the command as follows


    f=open(fname,"r")


Now for counting we will iterate through the file and read it character by character.  Then, we will check each character whether it is a uppercase character or lowercase or digit or space or special character and we will add one in the specific variable according to the category in which it lies in.
For more conceptual clarity, you can debug the file and observe the values of variables at each step.

I hope the logic for question would be clear to you. You are advised to try it first and then check the program source code which is given below.

You can run and code in python online on this website - Python Online Interpreter


Source Code

def Counting(fname):  #fname means filename
    uc=lc=d=s=sc=0
    f=open(fname,"r")
    for line in f:
        for c in line:
            if c>='A' and c<='Z':
                uc=uc+1
            elif c>='a' and c<='z':
                lc=lc+1
            elif c>'0' and c<='9':
                d=d+1
            elif c==' ':
                s=s+1
            else:
                sc=sc+1

    print("No of uppercase=",uc)
    print("No of lowercase=",lc)
    print("No of digits=",d)
    print("No of spaces=",s)
    print("No of special Character=",sc)

#-----Driver Code ----- Function Call-----
Counting("D:\\story.txt")

If you like the content, share it in your social media platforms so that we can help the maximum. If you have any query then feel free to ask in the comments below. We are ready to help you. Also, your appreciation is welcomed in comments.

Check Similar Programs Here - Python Programs - Question and Answers


Enjoy Coding!!




Keyword for the Post :-
python program to count the number of words in a text file, python program to count the number of lines in a text file, file handling in python, how to count number of words in a text file in python, how to count number of lines in a text file in python, python program to count number of spaces in a string, write a python program to count the number of words in a text file, how to count number of vowel present in text file

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post