In Python, strings are sequences of characters. The istitle()
method is a built-in method used to check whether the given string is in title case format or not. A string is said to be in title case format if the first character of each word in the string is in uppercase and the remaining characters are in lowercase.
Syntax
The syntax of the istitle()
method is as follows:
string.istitle()
Here, string
is the string to be checked if it is in title case format.
Return Value
The istitle()
method returns True
if the given string is in title case format. Otherwise, it returns False
.
Examples
Here are three different examples of how to use the istitle()
method in Python:
Example 1: Check if a string is in title case format
string = "This Is An Example" result = string.istitle() print(result)
Output:
True
In this example, we create a string “This Is An Example” and use the istitle()
method to check if it is in title case format. Since the first character of each word in the string is in uppercase and the remaining characters are in lowercase, the method returns True
.
Example 2: Check if a string is not in title case format
string = "this is not in title case format" result = string.istitle() print(result)
Output:
False
In this example, we create a string “this is not in title case format” and use the istitle()
method to check if it is in title case format. Since not all the first characters of each word in the string are in uppercase and the remaining characters are in lowercase, the method returns False
.
Example 3: Check if all strings in a list are in title case format
strings = ["This Is Title Case", "This is not", "Neither is this"] results = [string.istitle() for string in strings] print(results)
Output:
[True, False, False]
In this example, we create a list of strings and use the istitle()
method along with a list comprehension to check if each string is in title case format. The method returns True
for the first string and False
for the remaining strings.
Use Cases
The istitle()
method can be useful in a variety of scenarios, such as:
- Validating user input to ensure that it is in title case format
- Checking if a given string is a title or a heading
- Transforming a given string into title case format