Logic Building
# Take string input
s = input("Enter a string: ")
# Find longest word
words = s.split()
if len(words) > 0:
longest = max(words, key=len)
print(f"Longest word: {longest}")
else:
print("No words found")Output
Enter a string: Hello World Programming Longest word: Programming
Use max() with key=len to find longest.
Key Concepts:
- max() finds maximum
- key=len uses length for comparison
- Returns longest word