my_str='welcome to plus2net python section'
output=my_str.title()
print(output)
my_str='welcome #id 15of plus2net'
output=my_str.title()
print(output)
Output
Welcome To Plus2Net Python Section
Welcome #Id 15Of Plus2Net
This also changes the first char after any number of special chars ( check plus2Net )
What happens when the string is already in mixed case? title() still ensures that only the first letter of each word is capitalized:
my_str = 'pYTHON tUTORIAL'
output = my_str.title()
print(output) # Output: 'Python Tutorial'
This is useful when you want to format names or titles in proper case for user-facing applications:
full_name = 'john doe'
formatted_name = full_name.title()
print(formatted_name) # Output: 'John Doe'
When a word contains special characters, only the letter after the special character will be capitalized:
my_str = "welcome to plus2net's python"
print(my_str.title()) # Output: "Welcome To Plus2Net'S Python"
It's useful for standardizing book or article titles in publishing applications:
book_title = "harry potter and the goblet of fire"
print(book_title.title()) # Output: "Harry Potter And The Goblet Of Fire"
While title() capitalizes each word, capitalize() affects only the first word:
text = "hello world"
print(text.capitalize()) # Output: "Hello world"