What is String istitle() Function in Python

Python string istitle() is a built-in string handling function that returns True if the first letter of the words present in the string is uppercase and all the other letters of the word are lowercase. In all other cases, it returns False

Another essential point is that symbols and numbers are ignored when using the istitle() method on the string.

We also call the words with the first letter uppercase and all other letters as lowercase title cased.

Syntax

str.istitle()

Here str is the string variable, which is to be checked if it is title cased.

Parameters

The istitle() method doesn’t take any parameter and throws an error if any parameter is passed.

Return Value

The istitle() function returns true if the string is title-cased and returns false in all other cases.

Example programs on istitle() method

Example1: Write a program to show the working of the istitle() method.

# app.py

h1 = "Hello Boy"
h2 = "345Hello Girl"
h3 = "hello Boy"
h4 = "I Am A Barca Fan"
h5 = "I hate ReaL Madrid"
h6 = "The Above Two Strings Mean The Same"

print("String: ", h1, "istitle: ", h1.istitle())
print("String: ", h2, "istitle: ", h2.istitle())
print("String: ", h3, "istitle: ", h3.istitle())
print("String: ", h4, "istitle: ", h4.istitle())
print("String: ", h5, "istitle: ", h5.istitle())
print("String: ", h6, "istitle: ", h6.istitle())

Output

python3 app.py
String:  Hello Boy istitle:  True
String:  345Hello Girl istitle:  True
String:  hello Boy istitle:  False
String:  I Am A Barca Fan istitle:  True
String:  I hate ReaL Madrid istitle:  False
String:  The Above Two Strings Mean The Same istitle:  True

Example 2: Write a program to check whether or not two strings are title-cased. If they are, print the “YES” and “NO” in all other cases.

See the following code.

# app.py

h1 = "I Am The King"
h2 = "I Love Maths"
if(h1.istitle() and h2.istitle()):
    print("YES")
else:
    print("NO")

Output

python3 app.py
YES

Conclusion

Python istitle() returns True if the string is the title-cased string; otherwise, it returns False.

That’s it for this tutorial.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.