Python Practice › String Programs › Program 22

Longest all-capital word

EasyString Programs · 22 of 106

Write a Python program to find the longest word written in capital letters.

Advertisement
Input
"Learning PYTHON programming is FUN"
Output
PYTHON

Approach

  1. Filter words with isupper(), then take the longest.

Solution

Python
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.

More string programs

← All 106 String Programs

Leave a Comment