
Python's istitle() Method
Today we're looking at Python's istitle() method. This function checks whether a string is in 'title case' or not. It's used like this:
string.istitle()
This method is applicable to String objects and returns a boolean value True if the string follows the title case convention, and False if it doesn't.
So, what is this 'title case' convention exactly? A string is in title case if each word starts with a capital letter, with the exception of certain words such as simple conjunctions or prepositions.
Let's go through an example to make this clearer.
Imagine you assign a string to the variable 'text'
>>> text = "The father of my father"
You can then check if it's in title case with the istitle() method
>>> text.istitle()
The istitle() method will return False in this case because the initials of some words are lowercase.
False
Let's try a different string.
Assign this string to the variable 'text'.
>>> text = "The Father Of My Father"
And then check if it's in title case:
>>> text.istitle()
This time, the method returns True because the initials of all words are uppercase, as required in title case.
True
So, how can you convert a string into title case?
You can use Python's title() method.
Remember, the istitle() method is based on the English title case convention, which means that the first letter of each word is capitalized, excluding simple conjunctions and prepositions. If you're working with other languages, other rules will apply. Keep this in mind as you continue your journey with Python.