Question:

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

Show Hint

Always strip punctuation before checking characters.
This avoids false results due to trailing symbols like . or ,.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Below is a Python function that does this:

def display_vowel_words():
    vowels = "AEIOUaeiou"
    result = []
    with open("Report.txt", "r") as f:
        for line in f:
            words = line.split()
            for word in words:
                clean_word = word.strip(".,?!")
                if clean_word and clean_word[0] in vowels and clean_word[-1] in vowels:
                    result.append(clean_word)
    print(" ".join(result))
This function first defines a string vowels containing all vowels.
It opens the file "Report.txt" using with open() in read mode.
Each line is split into words using split().
Each word is cleaned with strip(".,?!") to remove punctuation.
It checks if the first and last letters are vowels.
If they are, the word is added to the result list.
Finally, all matching words are joined with a space and printed.
Was this answer helpful?
0
0