Python String istitle() Method
The istitle() method in Python checks whether each word in a string starts with an uppercase letter followed by lowercase letters. It returns True if the string is in title case.
Syntax
string.istitle()
Returns:
True– if all words are title-casedFalse– otherwise
Example 1: Valid Title Case
text = "Hello World"
print(text.istitle())
True
Example 2: Not a Title Case
text = "hello world"
print(text.istitle())
False
Example 3: Mixed Case
text = "Hello world"
print(text.istitle())
False
Use Cases
- Validating titles or headers in a document
- Checking formatting before applying transformations
- Filtering data for properly capitalized phrases
Important Notes
- Only alphabetic characters are considered. Numbers or symbols don't affect the result.
- Empty strings return
False.
Common Mistake
Words like "The USA" are not in title case (because "USA" is in all caps), so istitle() will return False.
Practice Problem
Write a program that reads a line of text from the user and checks if it's in title case:
text = input("Enter a sentence: ")
if text.istitle():
print("The text is in title case.")
else:
print("The text is NOT in title case.")
Summary
string.istitle()checks if each word starts with a capital letter.- Returns
Truefor title case,Falseotherwise. - Commonly used for validation in formatting.
Comments
Loading comments...