sentence = "Practice makes progress, not perfection!"
words = [w.strip(".,!?") for w in sentence.split()]
longest = max(words, key=len) # max returns the FIRST of equal maxima
print(longest, len(longest))
print(sorted(words, key=len, reverse=True)[:2])
Output Verified by running it on Python 3
perfection 10
['perfection', 'Practice']
strip(".,!?") removes punctuation at the ends of each word. max(…, key=len) compares lengths; sorted is stable, so "Practice" stays ahead of "progress".
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!