Editor

The Longest Word

#522 · Strings · Beginner · 25 XP · about 4 min

Write longest_word(sentence) that returns the longest word in sentence. If several words share the greatest length, return the first of them. If the sentence has no words at all, return an empty string.

The sentence contains only letters and spaces, and words are separated by one or more spaces.

Examples

longest_word('I love programming')  # returns 'programming'
longest_word('cat dog emu')         # returns 'cat'
longest_word('')                    # returns ''

In the second example all three words have 3 letters, so the first one wins.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.