Home »
Python »
Python programs
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 »