Check sibling questions

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()

  1. Computer Science - Class 12
  2. Solutions to CBSE Sample Paper - Computer Science Class 12

About the Author

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo