Find Longest Word

Find the longest word in a sentence.

PythonBeginner
Python
# Program to find the longest word in a sentence

sentence = input("Enter a sentence: ")

words = sentence.split()

if not words:
    print("No words found.")
else:
    longest = max(words, key=len)
    print("Longest word:", longest)

Output

Enter a sentence: Python string programs collection
Longest word: collection

We split the sentence into words and use max with key=len to find the longest one.