Python String title() Method
text.title() returns a new string in which the first letter of every word is uppercase and all other letters are lowercase.title() is a quick way to turn names, headings or labels into Title Case, such as "learn python" into "Learn Python". It has one well-known weakness: it treats any non-letter as a word boundary, so apostrophes and hyphens can give odd results. This page shows how it works and how to avoid the traps.
To capitalise only the very first character of a string, use capitalize() instead.
| Syntax | text.title() |
|---|---|
| Parameters | None |
| Returns | A new str in title case |
| Time complexity | O(n) in the length of the string |
How title() decides where words start
Python lower-cases every letter, then upper-cases each letter that follows a character that is not a letter. So spaces, digits, hyphens and apostrophes all start a new "word".
print("the quick brown fox".title())
print("hELLO wORLD".title())
print("route 66 is long".title())The apostrophe problem and how to avoid it
Because an apostrophe is not a letter, the letter after it is capitalised too. That gives "They'Re" and "Don'T". A reliable alternative is to split the text, capitalise each word, and join it back. See split() and join().
text = "don't stop, it's fun"
print(text.title())
print(" ".join(w.capitalize() for w in text.split()))title() vs capitalize() vs upper()
| Method | Effect | Result for "hello big world" |
|---|---|---|
title() | First letter of each word uppercase | 'Hello Big World' |
capitalize() | Only the first character uppercase | 'Hello big world' |
upper() | Every letter uppercase | 'HELLO BIG WORLD' |
istitle() | Checks title case, returns a bool | False |
string.capwords() | Splits on whitespace, capitalises each word | 'Hello Big World' |
Real title case in headings is more than title()
Style guides keep words like "of" and "the" in lowercase in the middle of a title. title() does not know that, so use it for names and simple labels, and apply custom rules for publishing-quality headings.
Examples you can run
Press Run, then edit the code and run it again.
Format a name in title case
Works well for simple names.
name = "asha RAO"
print(name.title())Expected outputAsha Rao
See the apostrophe pitfall and a fix
string.capwords() splits on whitespace so apostrophes are safe.
import string
phrase = "it's a student's book"
print(phrase.title())
print(string.capwords(phrase))Expected outputIt'S A Student'S Book
It's A Student's Book
Title-case each item in a list
Apply title() with a list comprehension.
courses = ["python basics", "data analysis", "web apps"]
print([c.title() for c in courses])Expected output['Python Basics', 'Data Analysis', 'Web Apps']
Common mistakes
Using title() on text with apostrophes
✗ Wrong
print("don't panic".title())✓ Right
import string
print(string.capwords("don't panic"))Use string.capwords() or capitalise each whitespace-separated word.
Expecting the original to change
✗ Wrong
city = "new delhi"
city.title()
print(city)✓ Right
city = "new delhi"
city = city.title()
print(city)Assign the result.
Expecting title() to keep existing capitals
✗ Wrong
print("NASA launches".title())✓ Right
words = ["NASA", "launches"]
print(" ".join(w if w.isupper() else w.capitalize() for w in words))title() lower-cases the rest of each word. Handle acronyms yourself.
Python String title() Method: questions people ask
How do I capitalise the first letter of every word in Python?
Use text.title() for simple text, or string.capwords(text) if the text contains apostrophes.
What is the difference between title() and capitalize()?
title() capitalises each word, while capitalize() capitalises only the first character of the whole string and lower-cases the rest.
Why does title() capitalise letters after an apostrophe?
Because it treats every non-letter character as the end of a word, so "don't".title() becomes "Don'T".
Keep learning
Step-by-step lessons:
Python StringsPython String Methods
Related:
Python String capitalize() MethodPython String upper() MethodPython String lower() MethodPython String swapcase() Method
More in string methods:
Python String split() MethodPython String join() MethodPython String strip() MethodPython String lstrip() MethodPython String rstrip() MethodPython String replace() MethodPython String upper() MethodPython String lower() Method See all →