Checking if first letter of string is in uppercase

Question:

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?

Asked By: Blücher

||

Answers:

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
Answered By: orlp

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
Answered By: Foo Bah

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
Answered By: AlG
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.