DocsHub
PythonAdvanced

Longest Word

Learn how to find the longest word in a sentence in Python.

Longest Word

Problem

Given a sentence, find the longest word. Handle ties — if two words have the same length, return the first one. Then extend to find the top N longest words.

Input:  "The quick brown fox jumps over the lazy dog"
Output: "quick"   (5 letters)

Input:  "I love Python programming"
Output: "programming"   (11 letters)

Input:  "cat bat hat"
Output: "cat"   (all same length — return first)

Input:  "Hello, world!"
Output: "Hello"   (punctuation removed)

Part 2 — top N longest words:

Input:  "The quick brown fox jumps", n=3
Output: ["quick", "jumps", "brown"]

Logic

Part 1:

  1. Clean the sentence — remove punctuation
  2. Split into words
  3. Track the longest word seen so far
  4. Loop through each word — if longer than current longest, update
  5. Return the longest

Part 2:

  1. Same cleaning and splitting
  2. Sort words by length — longest first
  3. Return the first N words

Solution 1 — using a loop

Loop through words and keep track of the longest seen so far.

def longest_word(sentence):
    # remove common punctuation
    for char in ".,!?;:\"'()-":
        sentence = sentence.replace(char, "")

    # split into words
    words = sentence.split()

    if not words:
        return ""

    # assume first word is the longest
    longest = words[0]

    for word in words[1:]:
        if len(word) > len(longest):
            longest = word   # found a longer word — update

    return longest


print(longest_word("The quick brown fox jumps over the lazy dog"))
# quick

print(longest_word("I love Python programming"))
# programming

print(longest_word("cat bat hat"))
# cat

print(longest_word("Hello, world!"))
# Hello

Code Execution — Solution 1

Trace through longest_word("The quick brown fox"):

After cleaning: "The quick brown fox" Words: ["The", "quick", "brown", "fox"]

Stepwordlen(word)len(longest)word > longest?longest
Start"The" (3)
1st"quick"53Yes"quick"
2nd"brown"55No — equal, keep first"quick"
3rd"fox"35No"quick"
Done"quick"

When "brown" ties with "quick" — we use strict > so the first longest is kept.


Solution 2 — using max() with key

Python's max() function accepts a key argument. Pass len as the key — find the word with maximum length.

def longest_word(sentence):
    # clean punctuation
    for char in ".,!?;:\"'()-":
        sentence = sentence.replace(char, "")

    words = sentence.split()

    if not words:
        return ""

    # max() with key=len finds the word with the most characters
    # if tie — max() returns the last one found
    # to get first, we use a manual approach or reverse trick
    return max(words, key=len)


print(longest_word("The quick brown fox jumps over the lazy dog"))
# jumps (max returns last longest — different from solution 1)

print(longest_word("I love Python programming"))
# programming

max() with key=len returns the last word when there is a tie — not the first. If you need the first longest word on ties, use Solution 1 (explicit loop) or Solution 3 below. Know your requirements before choosing.


Solution 3 — first longest on tie

Guarantee returning the first longest word even when max() would return the last.

def longest_word(sentence):
    for char in ".,!?;:\"'()-":
        sentence = sentence.replace(char, "")

    words = sentence.split()

    if not words:
        return ""

    longest = ""
    max_len = 0

    for word in words:
        # strictly greater than — skip ties, keeps first
        if len(word) > max_len:
            max_len = len(word)
            longest = word

    return longest


print(longest_word("cat bat hat"))           # cat — first of equal lengths
print(longest_word("hat cat bat"))           # hat — still first
print(longest_word("The quick brown fox"))   # quick — first of length 5

Code Execution — Solution 3

Trace through longest_word("cat bat hat"):

Words: ["cat", "bat", "hat"]

Stepwordlen(word)max_lenlen > max_len?longest
Start0""
1st"cat"30Yes"cat"
2nd"bat"33No — equal"cat"
3rd"hat"33No — equal"cat"
Done"cat"

Strict > means equal lengths never update longest — first one wins.


Part 2 — Solution 1: top N longest words using sort

Sort all words by length descending, return first N.

def top_n_longest(sentence, n):
    # clean punctuation
    for char in ".,!?;:\"'()-":
        sentence = sentence.replace(char, "")

    words = sentence.split()

    if not words:
        return []

    # remove duplicate words first — optional but cleaner
    seen = set()
    unique_words = []
    for word in words:
        if word.lower() not in seen:
            unique_words.append(word)
            seen.add(word.lower())

    # sort by length descending
    # key=lambda w: len(w) → sort by word length
    # reverse=True → longest first
    sorted_words = sorted(unique_words, key=lambda w: len(w), reverse=True)

    # return first n words
    return sorted_words[:n]


sentence = "The quick brown fox jumps over the lazy dog"

print(top_n_longest(sentence, 3))   # ['quick', 'brown', 'jumps']
print(top_n_longest(sentence, 5))   # ['quick', 'brown', 'jumps', 'over', 'lazy']
print(top_n_longest(sentence, 1))   # ['quick']

Code Execution — Part 2 Solution 1

Trace through top_n_longest("The quick brown fox jumps", n=3):

Words: ["The", "quick", "brown", "fox", "jumps"]

After deduplication (all unique already): same list

Sort by len descending:

WordLength
"quick"5
"brown"5
"jumps"5
"fox"3
"The"3

Sorted: ["quick", "brown", "jumps", "fox", "The"]

Return first 3: ["quick", "brown", "jumps"]


Part 2 — Solution 2: using heapq.nlargest

More efficient for large word lists — does not sort everything, just finds top N.

import heapq

def top_n_longest(sentence, n):
    for char in ".,!?;:\"'()-":
        sentence = sentence.replace(char, "")

    words = list(set(sentence.split()))   # unique words

    if not words:
        return []

    # nlargest finds top n by length without sorting everything
    return heapq.nlargest(n, words, key=len)


sentence = "The quick brown fox jumps over the lazy dog"

print(top_n_longest(sentence, 3))   # 3 longest unique words
print(top_n_longest(sentence, 5))   # 5 longest unique words

Code Execution — Part 2 Solution 2

heapq.nlargest(3, words, key=len) internally:

  1. Builds a min-heap of size 3
  2. Processes each word — if longer than smallest in heap, replace it
  3. Returns the 3 longest

This is O(n log k) instead of O(n log n) for full sort — faster when k (top N) is much smaller than n (total words).


Bonus — longest word with its length and position

Return not just the word but also where it appeared and how long it is.

def longest_word_info(sentence):
    for char in ".,!?;:\"'()-":
        sentence = sentence.replace(char, "")

    words = sentence.split()

    if not words:
        return None

    longest = max(words, key=len)
    position = words.index(longest) + 1   # 1-based position

    return {
        "word": longest,
        "length": len(longest),
        "position": position,
        "total_words": len(words)
    }


result = longest_word_info("I love Python programming very much")
print(f"Word:     {result['word']}")
print(f"Length:   {result['length']} characters")
print(f"Position: word {result['position']} of {result['total_words']}")

Output:

Word:     programming
Length:   11 characters
Position: word 4 of 6

Bonus — longest word that appears more than once

from collections import Counter

def longest_repeated_word(sentence):
    for char in ".,!?;:\"'()-":
        sentence = sentence.replace(char, "")

    words = sentence.lower().split()
    counts = Counter(words)

    # keep only words that appear more than once
    repeated = [word for word, count in counts.items() if count > 1]

    if not repeated:
        return None

    return max(repeated, key=len)


print(longest_repeated_word("the cat sat on the mat near the cat"))
# "the" appears 3 times, "cat" appears 2 times
# longest repeated = "cat" (3 chars) vs "the" (3 chars) → "cat" or "the"

Which solution to use?

SolutionHowBest when
Part 1 — Solution 1Loop + compareClear, returns first on tie
Part 1 — Solution 2max(key=len)Concise, returns last on tie
Part 1 — Solution 3Loop with max_lenReturns first on tie, explicit
Part 2 — Solution 1Sort descendingSmall to medium word lists
Part 2 — Solution 2heapq.nlargestLarge word lists, small N

Output

quick
programming
cat
Hello

['quick', 'brown', 'jumps']
['quick', 'brown', 'jumps', 'over', 'lazy']
['quick']

On this page