Find the Longest Word

Easy ⏱ 8 min 85% acceptance ★★★★☆ 4.3
Write longest_word(s) that returns the longest word in the sentence s. If there is a tie, return the first one that appears.

Examples

Example 1
Input
s = 'I love programming in python'
Output
'programming'
Explanation

'programming' has 11 characters, the most of any word.

Example 2
Input
s = 'cat dog owl bee'
Output
'cat'
Explanation

All words are 3 letters; the first one wins the tie.

Constraints

  • s has at least one word.

Topics

Stringssplit

Companies

AirbnbLinkedIn

Hints

Hint 1

Split into words, then use `max(words, key=len)` — `max` keeps the first maximal element on ties.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.