The Python str.capitalize() method returns a copy of the string with the first character in uppercase and every other character in lowercase. It suits sentences and single lowercase words, and it is often confused with title(), which capitalizes every word.
city = "new YORK"
capitalized = city.capitalize() # 'New york'
original = city # 'new YORK', strings are immutable
Notice that YORK became york, because capitalize() lowercases the rest of the string. Comparing it with title(), string.capwords() and upper() on one sentence shows which method to pick, and a few edge cases with digits, spaces and Unicode follow. Results are from Python 3.14.
1. Syntax and Return Value
The method takes no arguments and works on any string.
str.capitalize() -> str
It returns a new str with the first character in titlecase (uppercase for ordinary letters) and the rest in lowercase. The original string stays unchanged, and an empty string returns an empty string.
2. capitalize() vs title() vs capwords() vs upper()
Running all four on the same text shows the differences at a glance. The apostrophe shows a known quirk of title().
import string
text = "the world's best pizza"
a = text.capitalize() # "The world's best pizza"
b = text.title() # "The World'S Best Pizza"
c = string.capwords(text) # "The World's Best Pizza"
d = text.upper() # "THE WORLD'S BEST PIZZA"
| Method | Changes | Use it for |
|---|---|---|
| capitalize() | First character up, the rest down | Sentences, single words |
| title() | First letter of every word up | Simple titles without apostrophes |
| string.capwords() | Splits on spaces and capitalizes each word | Headlines with apostrophes (it also lowercases the rest of each word) |
| upper() | Every character up | Codes and labels |
The title() method treats every non-letter as a word boundary, so it turns world’s into World’S. The string.capwords() function splits only on whitespace and avoids that problem.
3. Strings That Do Not Start with a Letter
When the first character is a digit, a symbol or a space, nothing gets uppercased, but the rest of the string is still lowercased.
age = "25 years OLD".capitalize() # '25 years old'
spaced = " hello".capitalize() # ' hello', the space is the first character
clean = " hello".strip().capitalize() # 'Hello'
4. Uppercasing Only the First Letter
Names such as McDonald lose their inner capital letter with capitalize(). To change only the first character and keep the rest, we combine slicing with upper().
name = "mcDonald"
wrong = name.capitalize() # 'Mcdonald'
first_only = name[:1].upper() + name[1:] # 'McDonald'
The slice name[:1] also works for an empty string, where name[0] would raise IndexError.
5. Unicode Behavior
Since Python 3.8, capitalize() puts the first character into titlecase rather than uppercase. Titlecase is the form a letter takes at the start of a capitalized word; for most letters it is the uppercase letter, but a few characters change length or have a separate titlecase form.
a = "straße".capitalize() # 'Straße'
b = "ßtraße".capitalize() # 'Sstraße', Python maps ß to 'SS' in upper and titlecase
c = "džemal".capitalize() # 'Džemal', the two-letter character dž has its own titlecase form
6. Cleaning Up Names Typed into a Form
Names typed into a form arrive in any case, and neither capitalize() nor title() handles all of them. A small function that capitalizes each word with slicing keeps inner capitals and apostrophes intact.
def tidy_name(raw):
words = raw.strip().split()
return " ".join(w[:1].upper() + w[1:] for w in words)
n1 = tidy_name(" lokesh gupta ") # 'Lokesh Gupta'
n2 = tidy_name("ronald mcDonald") # 'Ronald McDonald'
n3 = tidy_name("o'neil") # "O'neil"
t3 = "o'neil".title() # "O'Neil"
The last two lines show that no single rule fits every name. For data that people see, such as invoices, we store the name the way the user typed it and only fix the first letter of each word, as tidy_name() does.
7. More Questions on Capitalizing Text
Capitalizing whole sentences and undoing capitals work a little differently from a single word.
7.1. Why does capitalize() not change our variable?
Strings are immutable, so the method returns a new string, and we assign the result back, as in name = name.capitalize().
7.2. How do we capitalize the first letter of each sentence?
We split on the sentence end, capitalize each part and join the parts again. Like capitalize() itself, the result lowercases names inside the sentences, so w[:1].upper() + w[1:] is safer for mixed text.
text = "first line. second line. third"
fixed = ". ".join(s.capitalize() for s in text.split(". ")) # 'First line. Second line. Third'
7.3. What is the opposite of capitalize()?
The lower() method lowercases the whole string, and casefold() does the same more aggressively for case-insensitive comparisons, for example it turns ß into ss.
8. Conclusion
The capitalize() method uppercases the first character and lowercases the rest, so it suits sentences but not names such as McDonald or titles. For titles we use string.capwords(), and to keep the rest of the string as it is, we uppercase only the first character with slicing.
9. References
Happy Learning !!