curriculum/challenges/english/blocks/daily-coding-challenges-python/68ffb91507a5b645769328c5.md
Given a sentence string, return the longest word in the sentence.
a-z, case-insensitive) count toward the word's length.longest_word("The quick red fox") should return "quick".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("The quick red fox"), "quick")`)
}})
longest_word("Hello coding challenge.") should return "challenge".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("Hello coding challenge."), "challenge")`)
}})
longest_word("Do Try This At Home.") should return "This".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("Do Try This At Home."), "This")`)
}})
longest_word("This sentence... has commas, ellipses, and an exclamation point!") should return "exclamation".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("This sentence... has commas, ellipses, and an exclamation point!"), "exclamation")`)
}})
longest_word("A tie? No way!") should return "tie".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("A tie? No way!"), "tie")`)
}})
longest_word("Wouldn't you like to know.") should return "Wouldnt".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(longest_word("Wouldn't you like to know."), "Wouldnt")`)
}})
def longest_word(sentence):
return sentence
import re
def longest_word(sentence):
words = sentence.split(" ")
longest = ""
for word in words:
cleaned = re.sub(r'[^a-zA-Z]', '', word)
if len(cleaned) > len(longest):
longest = cleaned
return longest