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.
string.istitle()
True
if the string is title-cased.False
if the string is not title-cased or empty.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.
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.
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
,”ABC
andDEF
are considered as separate words.
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.
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.
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