Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if first letter of string is in uppercase

Tags:

python

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?

like image 366
Blücher Avatar asked Sep 08 '11 20:09

Blücher


People also ask

How do you check if the first letter of a string is uppercase in Java?

Character. isUpperCase(char ch) determines if the specified character is an uppercase character. A character is uppercase if its general category type, provided by Character.

How do you check if the first letter of the string is a capital C++?

C++ isupper() The isupper() function in C++ checks if the given character is a uppercase character or not.

How do you check if the first letter of a word is uppercase in Python?

In Python, isupper() is a built-in method used for string handling. This method returns True if all characters in the string are uppercase, otherwise, returns “False”.

How do you check if a character is an uppercase letter?

isupper() – check whether a character is uppercase.


2 Answers

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
like image 143
AlG Avatar answered Oct 11 '22 19:10

AlG


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
like image 43
orlp Avatar answered Oct 11 '22 18:10

orlp