English

Write a Python function that displays the number of times the word “Python” appears in a text file named “Prog.txt”. - Computer Science (Python)

Advertisements
Advertisements

Question

Write a Python function that displays the number of times the word “Python” appears in a text file named “Prog.txt”. 

Code Writing
Advertisements

Solution

def count_python():
   count = 0
   with open("Prog.txt", 'r') as file:
     text = file.read()
     words = text.split()
     for word in words:
       if word.lower() == "python":
         count += 1
     print("The word Python appears", count, "times.")
shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Board Sample Paper
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×