Python Practice › String Programs › Program 22
Longest all-capital word
Write a Python program to find the longest word written in capital letters.
Input
"Learning PYTHON programming is FUN"
Output
PYTHON
Approach
- Filter words with
isupper(), then take the longest.
Solution
text = "Learning PYTHON programming is FUN"caps = [w for w in text.split() if w.isupper()]print(max(caps, key=len) if caps else None)Output
PYTHON
Line by line
Line 3
The conditional expression avoids an error when no word is in capitals.
Learn the concept
New to this topic? Read the lesson, then come back.