Checking if first letter of string is in uppercase
I want to create a function that would check if first letter of string is in uppercase. This is what I've came up with so far:
def is_lowercase(word):
if word[0] in range string.ascii_lowercase:
return True
else:
return False
When I try to run it I get this error:
if word[0] in range string.ascii_lowercase
^
SyntaxError: invalid syntax
Can someone have a look and advise what I'm doing wrong?
Solution 1:
Why not use str.isupper()
;
In [2]: word = 'asdf'
In [3]: word[0].isupper()
Out[3]: False
In [4]: word = 'Asdf'
In [5]: word[0].isupper()
Out[5]: True
Solution 2:
This is built-in for strings:
word = "Hello"
word.istitle() # True
but note that str.istitle
looks whether every word in the string is title-cased, so this might give you a surprise:
"Hello world".istitle() # returns False!
If you just want to check the very first character of a string use this:
word = "Hello world"
word[0].isupper() # True