8. Write a python program to identify the longest words in a file, a.txt.
The output is:
Original file a.txt:
Welcome to
www.freelearningpoints.com
You can learn Python tutorials and
practice with exercises
Expected Answer:
The longest words is ['www.freelearningpoints.com']
Sample Answer:
def longest_word(file_name): with open(file_name, 'r') as f: words = f.read().split() max_length = len(max(words, key=len)) return [word for word in words if len(word) == max_length] print("The longest words is", longest_word('a.txt')))
More Exercises:
Python String ExercisesMore Numpy Exercises:
Numpy String ExercisesMore Pandas Exercises:
Pandas Series ExercisesMore Tutorials:
Python Installation - Linux (Ubuntu)