Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 966
clayton33 Avatar asked Jan 15 '11 01:01

clayton33


People also ask

How do you check if a letter is capitalized in Python?

In Python, isupper() is a built-in method used for string handling. This method returns True if all characters in the string are uppercase, otherwise, returns “False”.

How do you check if there is a capital letter in a string?

To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!

How do you check if a character is uppercase or lowercase in python?

To check if a character is upper-case, we can simply use isupper() function call on the said character.


1 Answers

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) 
like image 52
Sam Dolan Avatar answered Oct 11 '22 06:10

Sam Dolan