Python String istitle() Method
The istitle()
method checks whether each word's first character is upper case and the rest are in lower case or not. It returns True if a string is titlecased; otherwise, it returns False. The symbols and numbers are ignored.
Syntax:
str.istitle()
Parameters:
None.
Return Value:
Returns boolean value True if the string is titlecased otherwise returns False.
The following example demonstrates the istitle()
method.
>>> greet='Hello World'
>>> greet.istitle()
True
>>> greet='Hello WORLD'
>>> greet.istitle()
False
>>> greet='hello world'
>>> greet.istitle()
False
>>> greet='HelloWorld'
>>> greet.istitle()
False
>>> s='Python Is A Programming Language'
>>> s.istitle()
True
If the first character is a number or special symbol, then it will be treated as a capital letter.
>>> addr='#1 Harbor Side'
>>> addr.istitle()
True
>>> addr='1 Central Park'
>>> addr.istitle()
True
An empty string, numeric string, or a string with only symbols are not considered titlecased.
>>> empt=''
>>> empt.istitle()
False
>>> numstr='100'
>>> numstr.istitle()
False
>>> usd='$'
>>> usd.istitle()
False
Generally, the istitle()
method is used with the title()
method to check a string before converting it in a titlecased, as shown below.
ustr=input()
if not ustr.istitle():
ustr = ustr.title()
print('Converted to TitleCase', ustr)
print('You entered a TitleCase string')