Question 7 - Past Year - 2 Mark Questions - Chapter 2 - File Handling in Python
Last updated at August 14, 2026 by Teachoo
A text file “PARA.txt” contains a paragraph. Write a function that searches for a given character and reports the number of occurrence of the character in the file.
Question 7 A text file “PARA.txt” contains a paragraph. Write a
function that searches for a given character and reports the number of occurrence of the character in the file.
declaring function count_alpha( )
Accepts a user input and assigns it to ‘x’
reading all the lines in the text file and assigning it to ‘l’
Iterating through each line in ‘l’ and assigning it to ‘line’
Iterating through each character in a word and assigning it to ‘char’
If a character matches, then increase the value of ‘c’ by 1
Invoking function count_alpha( )
Assigning a variable ‘c’ with value 0
Opening the text file in read mode and assigning it to file_object ‘f’
Iterating through each word in a line and assigning it to ‘word’
Checking if a character matches the value assigned to ‘x’
Prints the number of occurrences
Closing the file
Output
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.
Go ad-free with Teachoo Black
Adblock detected
Skip every ad. Keep every answer.
Teachoo Black gives you a clean, uninterrupted study experience on every page.