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 loo开发者_开发百科k and advise what I'm doing wrong?
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
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
The syntax error stems from the fact that you need parentheses:
range(string.ascii_lowercase)
But in fact you shouldn't use range. It's as simple as:
if word[0] in string.ascii_lowercase
精彩评论