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

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


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)


Another, more compact, way to do sdolan's solution in Python 2.7+

>>> test = "asdfGhjkl">>> print "upper" if any(map(str.isupper, test)) else "lower"upper>>> test = "asdfghjkl">>> print "upper" if any(map(str.isupper, test)) else "lower"lower


Use string.isupper() with filter()

>>> letters = "asdfHRbySFss">>> def isCap(x) : return x.isupper()>>> filter(isCap, myStr)'HRSF'