42

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?

Blücher
  • 823
  • 3
  • 9
  • 18

3 Answers3

81

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
Xantium
  • 11,201
  • 10
  • 62
  • 89
AlG
  • 14,697
  • 4
  • 41
  • 54
  • This is a good answer but I was looking to learn what I did wrong in the code I wrote rather than just look for a different solution to the same problem. Thank you anyway. – Blücher Sep 08 '11 at 20:48
  • @gameFace Good point. Sometimes the best solution is a different solution. ;) – AlG Sep 09 '11 at 11:41
  • @Blücher In that case, I suggest `Code Review`, instead of `Stack Overflow`. – Ryan Jun 10 '18 at 05:28
  • one way could be is to use split. if you are sure there would be multiple words with space as separator then this might be a solution: `w="Hello world w.split()[0].istitle()` – Swapnil B. Apr 30 '21 at 01:24
35

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
orlp
  • 112,504
  • 36
  • 218
  • 315
  • 1
    Visceral reaction: in the original post, before you edited, you didn't make it clear that phrases like "Hello world" and "HELlo" fail. istitle checks that every word satisfies the format of , and it wasn't clear if that was a restriction given by the OP. Rolled back downvote – Foo Bah Sep 08 '11 at 20:26
  • Thank you, I've seen this but wanted to create something myself rather than use istitle(). – Blücher Sep 08 '11 at 20:26
  • 1
    @gameFace: Why? If this is for an excersice then I doubt it's usefulness, but if it's for real code than it's downright bad. – orlp Sep 08 '11 at 20:27
  • 2
    @nightcracker It's for an exercise, I feel I learn more by creating something new (even if it's usefulness is doubtful) rather than by blindly using ready made solutions - at least at the stage I'm right now. – Blücher Sep 08 '11 at 20:35
  • 1
    Never use `istitle()` function. It can lead to strange hard to find bug. For example try it `"Father's".istitle()` and then imagine that you have to debug the wrong behavior without knowing what this is the problem. – Salvador Dali Sep 11 '15 at 09:59
-1

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
Foo Bah
  • 25,660
  • 5
  • 55
  • 79
  • 2
    Testing if it is in `ascii_lowercase` could lead to localization problems down the road -- better to use isupper(). For example, what if the word is in Cyrillic? The first character would be upper case, but it would not be in `ascii_lowercase`. – Alex Martini Sep 08 '11 at 20:25
  • 1
    `string.ascii_lowercase` is not an integer, and isn't a valid argument to `range()`. – Wooble Sep 08 '11 at 20:26
  • @Wooble explaining why the syntax error arose in the first place. Hence I wrote "you shouldn't use range" afterwards, to make sure I wasn't advocating using that solution – Foo Bah Sep 08 '11 at 20:27