Create longest_word_in_sentence.py

This commit is contained in:
Saurabh Mahapatra 2024-07-05 10:29:48 +05:30 committed by GitHub
parent c1dc8e97f7
commit 1c7710de38
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,19 @@
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'
"""
words = sentence.split()
return max(words, key=len) if words else ''
if __name__ == "__main__":
from doctest import testmod
testmod()