Skip to content
Question

Q.(a) Write a Python function that displays all the lines containing the word 'vote' from a text file "Elections.txt". For example, if the file contains : In an election many people vote to choose their representative. The candidate getting the maximum share of votes stands elected. Normally, one person has to vote once. The process of voting may vary with time and region. Then the output should be : In an election many people vote to choose their representative. Normally, one person has to vote once.

(OR)
(b) Write a Python function that displays all the words starting and ending with a vowel from a text file "Report.txt". The consecutive words should be separated by a space in the output. For example, if the file contains : Once there was a wise man in a village. He was an awesome story-teller. He was able to keep people anchored while listening to him. Then the output should be : Once a a awesome able
CBSECBSE Class XII Board 2025Subjective· 3mImportance★★★★★
🔒 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) prints only the file lines containing the whole word vote.

Part (b) prints every word that both starts and ends with a vowel, space-separated (Once a a awesome able).

Part (a)

The task is line filtering. Open Elections.txt in read mode, iterate line by line, and decide whether to print each line.

The key subtlety is in the sample: the second line ("... maximum share of votes ...") and the fourth line ("... process of voting ...") are NOT in the expected output, even though both contain the substring vote. So we must test for the whole word vote, not the substring. We do this by splitting the line into words with split(), stripping punctuation from each word, lowercasing, and checking membership.

def display_vote_lines():
    with open("Elections.txt", "r") as f:
        for line in f:
            words = [w.strip('.,!?;:').lower() for w in line.split()]
            if 'vote' in words:
                print(line.strip())

Trace on the sample:

  • Line 1: words include vote -> printed.
  • Line 2: word is votes (not vote) -> skipped.
  • Line 3: words include vote -> printed.
  • Line 4: word is voting (not vote) -> skipped.

Expected output:

In an election many people vote to choose their representative.
Normally, one person has to vote once. …

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.