Python String istitle()
Python String istitle() method
Python String istitle() method returns True if all the words in the given text start with an upper case and the rest of the characters in the words are lower case, or False otherwise.
Consider that given string is in x.
x = "Hello World"
Then the return value of x.istitle() is
True
Now consider another value in string x.
x = "Hello world"
Then the return value of x.istitle() is
False
In this tutorial, you will learn the syntax and usage of String istitle() method in Python language.
Syntax of istitle() method
The syntax of String istitle() method in Python is given below.
str.istitle()
Parameters
The string istitle() method takes no parameters.
Return value
The string istitle() method returns a boolean value.
Examples
1. Checking if given string is a title in Python
In this example, we take a string value in variable x. We have to check if the text in this string is a title or not.
Call istitle() method on the string object x and use the returned value as a condition in Python if else statement as shown in the following program.
Python Program
x = "Hello World"
if x.istitle():
print('Given string is a TITLE.')
else:
print('Given string is NOT a TITLE')
Since all the words in given string x start with an upper case, and the rest of the characters are lowercase, istitle() method returns True, and the if-block executes.
Output
Given string is a TITLE.
2. istitle() with string containing words that start with lowercase in Python
In the following program, we take a string value in variable x such that some of the words in the string does not start with an upper case. We have to check if x is a title string or not.
Python Program
x = "Hello world"
if x.istitle():
print('Given string is a TITLE.')
else:
print('Given string is NOT a TITLE')
The word world in "Hello world" does not start with an upper case, and therefore istitle() returned False.
Output
Given string is NOT a TITLE
3. istitle() with string containing all upper case characters in words in Python
In the following program, we take a string value in variable x such that all the characters in the words are upper case. We have to check if x is a title string or not.
Python Program
x = "HELLO WORLD"
if x.istitle():
print('Given string is a TITLE.')
else:
print('Given string is NOT a TITLE')
istitle() returns False if any of the characters in the words except for the first character are upper case.
Output
Given string is NOT a TITLE
Summary
In this tutorial of Python String Methods, we learned about String istitle() method, its syntax, and examples.