Characters counting in text file

f = open(‘temp.txt’,’r’)

d=0
u=0
l=0
s=0
sp=0
lines=0
content = f.read()


for ch in content:

    if ch.islower():
        l = l + 1
    elif ch.isupper():
        u = u + 1
    elif ch == ' ':
        s = s + 1

    elif ch.isnumeric():
        d = d + 1
    elif ch == '\n':
        lines = lines + 1     
    else:

        sp=sp + 1

print(‘Lower case ‘,l)
print(‘Upper case’,u)
print(‘Spaces ‘,s)
print(‘Digits ‘,d)
print(‘Special ‘,sp)
print(‘Lines ‘,lines)

Output:

Lower case 18
Upper case 4
Spaces 4
Digits 2
Special 1
Lines 2

input file

Good Morning Good Evening
23 *

Published by Python programming examples for beginners

Abhay Gadkari is an IT professional having around experience of 20+ years in IT industry. He worked on web technologies and databases with Insurance and ERP projects.