Skip to content
Question

Q.Write a function Show_words() in Python to read the content of a text file 'NOTES.TXT' and display the entire content in capital letters. Example, if the file contains: "This is a test file" Then the function should display the output as: THIS IS A TEST FILE

(OR)
Write a function Show_words() in Python to read the content of a text file 'NOTES.TXT' and display only such lines of the file which have exactly 5 words in them. Example, if the file contains: This is a sample file. The file contains many sentences. But needs only sentences which have only 5 words. Then the function should display the output as: This is a sample file. The file contains many sentences :
CBSECBSE Class XII Board 2020Subjective· 2mImportance★★★★★
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Part (a): read the whole file and print it in uppercase (upper()).

Part (b): print only the lines that contain exactly 5 words (len(line.split())==5).

Part (a)

Open NOTES.TXT in read mode, read its entire content with read(), convert to capitals with the string method upper(), and print it. The with block closes the file automatically.

def Show_words():
    with open('NOTES.TXT', 'r') as f:
        content = f.read()
        print(content.upper())

For This is a test file:

THIS IS A TEST FILE …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.