intermediate

Find Longest Word

Find the longest word in a sentence.

Practice string splitting and length comparison.

📚 Concepts & Theory

Finding Max Length

max(words, key=len)

🎯 Your Challenge

Write longest_word that returns the longest word.

📝 Starter Code

Python
def longest_word(sentence):
    pass

print(longest_word('The quick brown fox'))

Solution

Python
def longest_word(sentence):
    words = sentence.split()
    longest = ''
    for word in words:
        if len(word) > len(longest):
            longest = word
    return longest

print(longest_word('The quick brown fox'))

Explanation

Track longest word by length.

❓ Frequently Asked Questions

max(words, key=len)

🔗 Related Concepts