Python String istitle() Method
The str.istitle()
method returns True
if all the words in the string are title cased (the first character of each word is in uppercase); otherwise, it returns False
.
Example:
text = "Python"
print(text.istitle())
Output
True
What is a title case?
Title case is a style of convention mainly used in headings, subheadings, titles, subtitles, etc. In the title case, all the words are capitalized, meaning the first character of each word starts with an uppercase, and the rest of them are in lowercase.
Example of Title Case
- Lord Of The Rings
- Inception
- Game Of Thrones
- The Matrix
Syntax
The Syntax of the str.istitle()
function is:
str.istitle()
Here, str
is a string.
Parameters
The str.istitle()
function does not take any parameters.
Return Value
The str.istitle()
function returns:
True
- if all the words in the string are title cased.False
- if at least one of the words in the string is not title cased or if it's an empty string,
Example 1: How istitle() method works?
In the below example, the istitle()
method returns True
if all the words in a string are title-cased and returns False
if one of the words in a string is not title case.
Note: The istitle()
method ignores the numbers and special characters when it finds in a string
# the first letter of the word is capital
text = "Inception"
print(text.istitle())
# the first letter of each word is capital
text = "Game Of Thrones"
print(text.istitle())
# string with numbers
text = "Apollo 13"
print(text.istitle())
# string starting with numbers
text = "12 Years Of Slave"
print(text.istitle())
# string in special characters
text = "2001: A Space Odyssey!!!"
print(text.istitle())
# In case of a single uppercase character
text = "A"
print(text.istitle())
# some words are not capitalized
text = "Lord of the Rings"
print(text.istitle())
# string in uppercase
text = "INTERSTELLAR"
print(text.istitle())
Output
True
True
True
True
True
True
False
False
Example 2: How to use the istitle() method in a Program?
# Method to verify if the sentence is titlecase or not
def check_titlecase(text):
if text.istitle() == True:
return "The sentence is titlecased"
else:
return "The sentence is not titlecased"
# title case sentenced
print(check_titlecase("Lord Of The Rings"))
# non-title case sentenced
print(check_titlecase("Lord of the Rings"))
Output
The sentence is titlecased
The sentence is not titlecased
Reference: Python Official Docs