What is string.istitle() in Python?
The istitle() method will check if a string is a True if the string is title cased. Otherwise, it returns False. If the string is empty, then False is returned.
Syntax
string.istitle()
Return value
Trueif the string is title-cased.Falseif the string is not title-cased or empty.
Example 1
string = "Test Test";
string.istitle(); # true
The string Test Test is a title-cased string because the first character of each word of the string is in uppercase, and the remaining characters are in lower case, so True is returned.
Example 2
string = "Test test"
string.istitle() # False
The string Test test is not a title-cased string because the first character of the second word (test) is not in uppercase, so False is returned.
Example 3
string = "T"
string.istitle() # True
The string T is considered title-case because it only has only one character, and that is an uppercase character.
Internally, the words of the string are separated with the regex
[A-Za-z]+, meaning when a non-alphabetical character occurs in a string, the next part is considered as a separate word. For example, in the string “ABC12DEF,”ABCandDEFare considered as separate words.
Example 4
string = "1T"
string.istitle() # True
The string above, 1T, is a title-cased string because in the string, “T” is considered a separate word. The T is title-case because the word T starts with an uppercase character.
Example 5
string = "1T2t"
string.istitle() # False
The string above, 1T2t, is not title-cased because “T” and “t” are considered separate words. The T is title-case because the word T starts with uppercase character, but “t” is not title-cased because the word doesn’t start with an uppercase character.
Complete example
string = "Test Test";print(string, " title cased -- ", string.istitle()) #Truestring = "Test test"print(string, " title cased -- ", string.istitle()) #Falsestring = "T"print(string, " title cased -- ", string.istitle()) #Truestring = "1T"print(string, " title cased -- ", string.istitle()) #Truestring = "1T2t"print(string, " title cased -- ", string.istitle()) #False