Open a text file and find the longest word in the text file and find the length.

By Subham, 7 months ago
  • Bookmark
0

Open a text file from PC and find the longest word in the text file and find the length.

File
Python
1 Answer
0

def longest_word(filename):
  with open(filename, 'r') as infile:
    words = infile.read().split()
  max_len = len(max(words, key = len))
  return [word for word in words if len(word) == max_len]

print(longest_word('Your_file_name.txt'))

Your Answer

Webinars

Live Masterclass on : "How Machine Learns in Machine Learning"

Dec 9th (6:00 PM) 224 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More