Python Reference ยท String Methods
Python str.istitle() method
Reference for Python str.istitle(): check whether words start with a capital and continue in lower case, how apostrophes and hyphens behave, and the link to title().
The istitle() method returns True if the string is title-cased: each run of letters starts with an upper-case letter and continues with lower-case letters only. Any character that is not a letter, such as a space, digit, hyphen or apostrophe, ends a run, so the next letter must be capitalized again.
It tests the same pattern that title() produces, but only for strings that contain at least one cased character.
print("Hello World".istitle()) # True print("Hello world".istitle()) # False
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
str.istitle()
This method takes no parameters.
Return value: True if the string has at least one cased character, upper-case letters only follow uncased characters, and lower-case letters only follow cased ones; otherwise False.
Raises: nothing.
Usage notes
- Needs a letter.
"123".istitle()and"".istitle()areFalse. - Every word counts. One lower-case word start, as in
"Hello world", makes the resultFalse. One capital inside a word, as in"HeLlo", also does. - All caps fails.
"NASA".istitle()isFalsebecause only the first letter of a run may be upper case. - Apostrophes split words.
"They're".istitle()isFalse, because therafter the apostrophe starts a new run and is not capitalized, and"They'Re"isTrue. This matches the odd output oftitle(), which turns"they're"into"They'Re". - Better alternative for real text.
string.capwords()splits on whitespace and capitalizes each word, giving"They're". Its result does not always satisfyistitle(). - Digits and symbols are uncased, so
"Python 3 Rocks".istitle()isTrue. - Title-case letters. Special characters like
วare title-case themselves and are treated as upper case here.