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]: True
In [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()):
       return True
elif(x[0].islower()):
       return False

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