Write a function ETCount() in Python, which should read each character of a text file “TESTFILE.TXT” and then count and display the count of occurrence of alphabets E and T individually (including small cases e and t too).

Example:

If the file content is as follows:

Today is a pleasant day.

It might rain today.

It is mentioned on weather sites

The ETCount() function should display the output as:

E or e: 6

T or t : 9

 

Answer:

def ETCount ():

    countE= 0

    countT= 0

    f=open( 'TESTFILE.txt','r' )

    lines=f.readlines()

    for line in lines:

        for ch in line:

            if ch== 'E' or ch== 'e' :

                countE=countE+1

            if ch== 'T' or ch== 't' :

                countT=countT+ 1    

    print( "Count of E/e: " ,countE)

    print( "Count of T/t: ", countT)

    f.close()       

 

ETCount()

Question 27(Choice 1) -Teachoo.png

Slide2.JPG

Remove Ads
Teachoo · Class 12 Explore Class 12
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh is an IIT Kanpur graduate and has been teaching for 16+ years. At Teachoo, he breaks down Maths, Science and Computer Science into simple steps so students understand concepts deeply and score with confidence.

Many students prefer Teachoo Black for a smooth, ad-free learning experience.