str.capitalize()
Returns a copy of a string with its first character in capitals and all the other letters turned into small letters.
str.capitalize()capitalize() makes the first character of the string a capital and every other letter small: "hELLO wORLD" becomes "Hello world".
Use it to tidy a single word or the start of a sentence, such as a name typed in the wrong case.
Note the second half of the rule: it does not just raise the first letter, it also lowers all the others. "iPhone" or "NASA rocket" lose their capitals. If the string starts with a space or digit, nothing becomes a capital.
Return value
A new string with only the first character in capitals.
Examples
1Capitalise a word
print("python".capitalize())
print("hELLO wORLD".capitalize())Python Hello world
2Tidy a list of first names
names = ["ana", "OMAR", "yUKI"]
print([n.capitalize() for n in names])['Ana', 'Omar', 'Yuki']
3The rest of the string gets lower-cased
print("NASA launches a rocket".capitalize())Nasa launches a rocket
To raise only the first letter and keep the rest, use s[:1].upper() + s[1:].
4Capitalise the first letter only
s = "nASA launches a rocket"
print(s[:1].upper() + s[1:])
print(" 2 apples".capitalize())NASA launches a rocket 2 apples
A string starting with a space or digit has no letter to raise.
Common mistakes
Using capitalize() for every word
It only affects the first character of the whole string. For each word, use title() or string.capwords().
city = "new york"
print(city.capitalize())
print(city.title())New york New York
Questions people ask
How do I capitalise each word?
Use str.title() or string.capwords(text).
How do I capitalise the first letter without changing the rest?
Use text[:1].upper() + text[1:].
Does capitalize() work with other alphabets?
Yes: "όλα".capitalize() gives "Όλα".