Given any string in Python, how can I test to see if its first letter is a capital letter? For example, given these strings:
January
dog
bread
Linux
table
I want to be able to determine that January
, and Linux
are capitalized.
In [48]: x = 'Linux'
In [49]: x[0].isupper()
Out[49]: True
In [51]: x = 'lINUX'
In [53]: x[0].isupper()
Out[53]: False
You can use something nice:
string = "Yes"
word.istitle() # -> True
but note that str.istitle looks whether every word in the string is title-cased! so it will only work on on 1 string in your case :)
"Yes no".istitle() # -> False!
If you just want to check the very first character of a string use KillianDS Answer...
if(x[0].isupper()):
return True
elif(x[0].islower()):
return False