PythonString methods
str. title()
Returns a string in which every word starts with an uppercase letter.
- Returns
- A new string with an uppercase letter at the start of each word.
- Changes the original
- No
Example
#Python
print("jOHN smith-walker".title())
print("chicago".title())
print("it's done".title())Resultsaved result, you can check it
John Smith-Walker
Chicago
It'S Done
Definition and usage
#The title() method converts the first letter of every word to uppercase and the rest to lowercase. It is handy for normalizing names typed in different ways.
It treats any character right after a non-letter character as the start of a word, so apostrophes and hyphens give unexpected results: "o'neil".title() is "O'Neil", but "it's".title() is "It'S".
Not every heading should capitalize every word, some style guides prefer capitalizing only the first word (sentence case). For that, capitalize() fits better than title().
Syntax
#Syntax
string.title()Good practices
#- For first and last names,
title()is usually enough, but watch out for names with apostrophes. - For more precise word formatting,
string.capwords()splits only on spaces.
Related entries
#Spotted a mistake or missing an example? Write to us.