How can I check if a letter in a string is capitalized using python?

clayton33 picture clayton33 · Jan 15, 2011 · Viewed 91k times · Source

I have a string like "asdfHRbySFss" and I want to go through it one character at a time and see which letters are capitalized. How can I do this in Python?

Answer

Sam Dolan picture Sam Dolan · Jan 15, 2011

Use string.isupper()

letters = "asdfHRbySFss"
uppers = [l for l in letters if l.isupper()]

if you want to bring that back into a string you can do:

print "".join(uppers)