Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a character is upper-case in Python?

Tags:

python

string

I have a string like this

>>> x="Alpha_beta_Gamma" >>> words = [y for y in x.split('_')] >>> words ['Alpha', 'beta', 'Gamma'] 

I want output saying X is non conformant as the the second element of the list words starts with a lower case and if the string x = "Alpha_Beta_Gamma" then it should print string is conformant

like image 272
lisa Avatar asked Sep 08 '10 14:09

lisa


People also ask

How do you check if a character is an uppercase letter?

isUpperCase(char ch) determines if the specified character is an uppercase character. A character is uppercase if its general category type, provided by Character. getType(ch), is UPPERCASE_LETTER. or it has contributory property Other_Uppercase as defined by the Unicode Standard.

How do you check if a character in a string is uppercase?

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!


2 Answers

To test that all words start with an upper case use this:

print all(word[0].isupper() for word in words) 
like image 163
Cristian Ciupitu Avatar answered Sep 20 '22 08:09

Cristian Ciupitu


Maybe you want str.istitle

>>> help(str.istitle) Help on method_descriptor:  istitle(...)     S.istitle() -> bool      Return True if S is a titlecased string and there is at least one     character in S, i.e. uppercase characters may only follow uncased     characters and lowercase characters only cased ones. Return False     otherwise.  >>> "Alpha_beta_Gamma".istitle() False >>> "Alpha_Beta_Gamma".istitle() True >>> "Alpha_Beta_GAmma".istitle() False 
like image 44
Jochen Ritzel Avatar answered Sep 22 '22 08:09

Jochen Ritzel