Skip to content Skip to sidebar Skip to footer

How Can I Test If A String Starts With A Capital Letter?

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 de

Solution 1:

In [48]: x ='Linux'In [49]: x[0].isupper()
Out[49]: TrueIn [51]: x ='lINUX'In [53]: x[0].isupper()
Out[53]: False

Solution 2:

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...

Solution 3:

if(x[0].isupper()):
       returnTrueelif(x[0].islower()):
       returnFalse

Post a Comment for "How Can I Test If A String Starts With A Capital Letter?"