210. Python Program to Find the Longest Word in a Sentence
MediumPhase 2 · Practice
This takes a few steps with tools you know: split(), len() and a for loop.
Overview
Finding the longest word is a common interview warm-up about strings. Word games and spell checkers look at word lengths all the time.
Problem Statement
Emeka plays a word game with a class. Each player says a sentence, and the longest word in it wins a point.
- A word is a piece of the sentence between spaces. A sign like
,or.counts as part of the word next to it. - If two or more words are the longest, the first one wins.
- Print two lines:
Longest word: <word>andLength: <number>.
Examples
Example 1. Input I like big elephants gives:
Longest word: elephants
Length: 9
Why: elephants has 9 letters. Every other word is shorter.
Example 2. Input red blue pink gray gives:
Longest word: blue
Length: 4
Why: blue, pink and gray all have 4 letters. blue comes first, so it wins.
Input
One line of words, with one space between them (1 word or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
Two lines: Longest word: <word> and Length: <number>. The tests try 8 different inputs, also one word and ties.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.