Files
Python/strings/longest_word_in_sentence.py
T
Saurabh Mahapatrapre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>Christian Clausscclauss
b021a69902 Create longest_word_in_sentence.py (#11475)
* Create longest_word_in_sentence.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update longest_word_in_sentence.py

* Update longest_word_in_sentence.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update longest_word_in_sentence.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update longest_word_in_sentence.py

* updating DIRECTORY.md

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: cclauss <cclauss@users.noreply.github.com>
2026-09-23 07:42:26 +02:00

31 lines
849 B
Python

def longest_word(sentence: str) -> str:
"""
Finds the longest word in a sentence.
>>> longest_word("The quick brown fox jumped over the lazy dog")
'jumped'
>>> longest_word("Python is amazing")
'amazing'
>>> longest_word("")
''
>>> longest_word("a")
'a'
>>> longest_word("A journey of a thousand miles begins with a single step")
'thousand'
>>> longest_word("To be or not to be that is the question")
'question'
>>> longest_word("Beauty is in the eye of the beholder")
'beholder'
>>> longest_word("A picture is worth a thousand words")
'thousand'
>>> longest_word("All that glitters is not gold")
'glitters'
"""
words = sentence.split()
return max(words, key=len) if words else ""
if __name__ == "__main__":
from doctest import testmod
testmod()