Python String istitle()

Created with Sketch.

Python String istitle()

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()

 

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"

 

However, the following string is title cased.

"They'Re"

 

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)

 

Output:

True

 

However, the following example returns False because the string contains an apostrophe ('):

note = "Jane's Books"

is_title = note.istitle()
print(is_title)

 

Output:

False

 

Summary

  • Use the Python string istitle() method to check if a string is title cased.

Leave a Reply

Your email address will not be published. Required fields are marked *