In this tutorial, we will be exploring the Python string method
istitle(). This is a built-in method in Python that checks if each word in a text starts with an uppercase letter and the rest of the word is in lowercase.
Usage of istitle() Method
The syntax for using the
istitle() method is as follows:
This method does not take any parameters and returns True if the string meets the condition, otherwise it returns False.
An Example of istitle() Method
text = "Hello World" result = text.istitle() print(result) # Output: True
In this example, since both ‘Hello’ and ‘World’ start with an uppercase letter followed by all lowercase letters,
istitle() returns True.
A Counter-Example of istitle() Method
text = "Hello world" result = text.istitle() print(result) # Output: False
In this counter-example, ‘world’ starts with a lowercase letter which makes
istitle() return False.
Tips for Using istitle() Method Effectively
istitle()function considers punctuation and whitespace as word delimiters. For instance, in “It’s A Beautiful Day”, each word separated by space or apostrophe is checked individually.
If you want to check if a sentence or title is properly capitalized according to English grammar rules (i.e., certain words like ‘a’, ‘an’, ‘the’ should not be capitalized unless they are the first word),
istitle()may not work as expected. In such cases, you might need to use a more complex method or a library that considers these rules.
The Python string method
istitle() is a simple and effective way to check if each word in a text starts with an uppercase letter followed by lowercase letters. It’s important to remember that it treats punctuation and whitespace as word delimiters, and does not consider English capitalization rules for certain words.