World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Open a text file from PC and find the longest word in the text file and find the length.
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'))