String istitle() Method in Python

Python String istitle() Method

The istitle() method in Python is used to check whether a string is in title case. A string is in title case if the first character of each word is in uppercase, and all the other characters in the word are in lowercase. It returns True if the string is in title case and there is at least one cased character (i.e., it does not return True for an empty string). Otherwise, it returns False.Here's the syntax of the istitle() method:

string.istitle()

Example:

# Examples of istitle() method
string1 = "Title Case Example"
string2 = "Not Title Case"
string3 = "Title Case With 123"
string4 = "This Is A Title"

result1 = string1.istitle()
result2 = string2.istitle()
result3 = string3.istitle()
result4 = string4.istitle()

print("String 1:", string1)
print("String 2:", string2)
print("String 3:", string3)
print("String 4:", string4)

print("istitle() result for String 1:", result1)
print("istitle() result for String 2:", result2)
print("istitle() result for String 3:", result3)
print("istitle() result for String 4:", result4)

Output:

String 1: Title Case Example
String 2: Not Title Case
String 3: Title Case With 123
String 4: This Is A Title
istitle() result for String 1: True
istitle() result for String 2: False
istitle() result for String 3: True
istitle() result for String 4: True

In this example, string1 and string4 are in title case, as the first character of each word is uppercase and the rest of the characters are lowercase, so istitle() returns True for both. string2 is not in title case as it contains words with all lowercase characters, so istitle() returns False. string3 contains numbers and is not entirely in title case, so it also returns False.

The istitle() method is useful when you want to check if a string is formatted in title case or when you need to validate user input for specific text formatting requirements. It can be used in applications where proper capitalization of words is important, such as in text editors, form input validation, and data cleaning tasks.