Summary: in this tutorial, you’ll learn how to use the Python string istitle()
method to check if a string follows the title case rules.
Introduction to the Python String istitle() method
The following show the syntax of the string istitle()
method:
str.istitle()
Code language: CSS (css)
The string istitle()
method returns True
if the string has at least one character and follows the title case rules. Otherwise, it returns False
.
Python uses very simple title case rules that the first character of each word in the string is uppercase while the remaining characters are lowercase.
However, Python uses the apostrophe (‘) as word boundaries. Therefore, the following string is not title cased:
"They're"
Code language: JSON / JSON with Comments (json)
However, the following string is title cased.
"They'Re"
Code language: JSON / JSON with Comments (json)
This behavior is what you may not expect.
Python String istitle() method examples
Let’s take some examples of using the string istitle()
method.
The following example uses the istitle()
to check if a string follows the title case rules:
name = 'Jane Doe'
is_title = name.istitle()
print(is_title)
Code language: PHP (php)
Output:
True
Code language: PHP (php)
However, the following example returns False
because the string contains an apostrophe ('
):
note = "Jane's Books"
is_title = note.istitle()
print(is_title)
Code language: PHP (php)
Output:
False
Code language: PHP (php)
Summary
- Use the Python string
istitle()
method to check if a string is title cased.