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"
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.