Find Longest Word
Find the longest word in a sentence.
BeginnerTopic: String Programs
Python Find Longest Word Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# 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
Understanding Find Longest Word
We split the sentence into words and use max with key=len to find the longest one.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.