title() returns a string in title case: the first letter of every word is upper case and the rest of the letters are lower case. "the python handbook" becomes "The Python Handbook".
A "word" for title() is any run of letters, and any character that is not a letter starts a new word. That includes apostrophes and digits, which causes a well known surprise: "they're" becomes "They'Re". The original string is never changed.
text.title() is a method of the built-in str type: call it on a string with dot notation. No import is needed, and it never changes the original string.
Syntax
Call it on any string. There are no arguments, but the parentheses are required.
python
result = text.title()
Call
What it does
"hello world".title()
Returns 'Hello World'.
"they're bill's friends".title()
Returns "They'Re Bill'S Friends". Each apostrophe starts a new "word", so the letter after it becomes a capital.
"3rd place".title()
Returns '3Rd Place'. A digit is not a letter, so the letter after it starts a word.
For names with apostrophes use string.capwords() from the standard library, which splits on spaces only. To capitalize only the first letter of the whole text use capitalize(), and to test for title case use istitle().
Library declarations
python
str.title() -> str
# returns: a new string; the original is unchanged
# raises: nothing (works for any string)
Parameters
title() takes no arguments. It always works on the whole string.
Return Value
Return Value
Description
str
A new string in title case. The original string is not changed.
Python Examples
Example 1 — Turn a Phrase Into a Title
Convert a lower case phrase and a shouted phrase to title case.
python
book = "the python handbook"
shout = "LEARN PYTHON TODAY"
print(book.title())
print(shout.title())
print(book)
Output
The Python Handbook
Learn Python Today
the python handbook
How It Works
Each word starts big.title() turns the first letter of every word into a capital.
The rest becomes small. In the shouted text, the other letters are changed to lower case, so "LEARN" becomes "Learn".
The original is unchanged.book still prints in lower case, because the method returns a new string.
Example 2 — Fix the Apostrophe Problem
See what title() does with apostrophes, and compare it with string.capwords().
python
import string
text = "they're here, and it's late"
print(text.title())
print(string.capwords(text))
Output
They'Re Here, And It'S Late
They're Here, And It's Late
How It Works
The trap.title() treats each apostrophe as the end of a word, so it prints They'Re and It'S.
A better tool.string.capwords() splits the text on spaces only, so the apostrophes cause no trouble.
Watch the comma. The word here, keeps its comma, which belongs to the word because capwords splits on spaces only.
Example 3 — Format a List of Names
Clean names that arrive in different cases and print them in one style.
python
names = ["ada lovelace", "ALAN TURING", "gRACE hOPPER"]
for name in names:
print(name.title())
print("Ada Lovelace".istitle(), "ADA LOVELACE".istitle())
Output
Ada Lovelace
Alan Turing
Grace Hopper
True False
How It Works
Three messy inputs. The names arrive in lower case, upper case, and mixed case.
One call fixes all.title() forces the capital-then-small pattern on each word, so the output is consistent.
Check the pattern.istitle() is True for "Ada Lovelace" and False for the shouted version.
Frequently Asked Questions
It returns a copy of the string in which the first letter of each word is upper case and all other letters are lower case.
title() starts a new word after every character that is not a letter, including apostrophes. Use string.capwords() when the text contains apostrophes.
title() capitalizes every word. capitalize() capitalizes only the first letter of the whole string and makes the rest lower case.
No. Strings are immutable, so title() returns a new string. Assign the result to keep it.
Call istitle(), which returns True when every word starts with a capital and the other letters are lower case.