Computer >> Computer tutorials >  >> Programming >> Python

How can I test if a string starts with a capital letter using Python?


To be precise, above string four words starting with uppercase character. This, Delhi, City and India.

Two string functions should be used for this purpose. Assuming that words in the string are separated by single space character, split() function gives list of words.

Secondly to check if first character of each word is uppercase, use isupper() function.

Following code lists words starting with capital letters.

s1="This is not true that Delhi is the hottest or coldest City in India"
for word in s1.split():
    if word[0].isupper():
        print (word)

The output is:

This
Delhi
City
India