Skip to content

Commit 1c7710d

Browse files
authored
Create longest_word_in_sentence.py
1 parent c1dc8e9 commit 1c7710d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

strings/longest_word_in_sentence.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def longest_word(sentence: str) -> str:
2+
"""
3+
Finds the longest word in a sentence.
4+
5+
>>> longest_word("The quick brown fox jumped over the lazy dog")
6+
'jumped'
7+
>>> longest_word("Python is amazing")
8+
'amazing'
9+
>>> longest_word("")
10+
''
11+
>>> longest_word("a")
12+
'a'
13+
"""
14+
words = sentence.split()
15+
return max(words, key=len) if words else ''
16+
17+
if __name__ == "__main__":
18+
from doctest import testmod
19+
testmod()

0 commit comments

Comments
 (0)