ADVERTISEMENT
ADVERTISEMENT

Python program to count total number of uppercase and lowercase characters in file

Here, we are going to learn how to count total number of uppercase and lowercase characters in file in Python?
Submitted by Shivang Yadav, on March 15, 2021

Programs can interact with files and data using the concept of file handling.

File Handling in Python is reading and writing data from files.

We will first read characters from the file and then character check whether it is uppercase (using isupper() method ) or lowercase (using islower() method ) and count its respective number and print the count.

Program to read data from files and count uppercase and lowercase character

# Program to count total number of 
# uppercase and lowercase characters in file

# Start of try block
try:
    #Counter for characters...
    upperCount = 0
    lowerCount = 0

    F=open("file.dat","r")
    while(True):
        data=F.read(1)
        if(data==""):
            break
        if (data.isupper()): 
                upperCount = upperCount + 1
        elif (data.islower()): 
                lowerCount = lowerCount + 1
                
    print(data,end='')
        
    print("Total Upper Case:",upperCount)
    print("Total lower Case:",lowerCount)
except FileNotFoundError as e:
    print(e)
finally:
    F.close()

File : File.dat

Learn Programming At IncludeHelp

Output:

Total Upper Case: 5
Total lower Case: 24

Python file handling programs »



ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.