Python String capitalize() Method
text.capitalize() returns a new string with the first character in uppercase and all remaining characters in lowercase.Use capitalize() to format a sentence, a single name or a label so that only its first letter is a capital. Note that it also lower-cases everything after the first character, which is why "PYTHON" becomes "Python".
If you want every word capitalised, use title() instead. If you only want to change the first letter and keep the rest, use slicing, as shown below.
| Syntax | text.capitalize() |
|---|---|
| Parameters | None |
| Returns | A new str |
| Time complexity | O(n) in the length of the string |
How capitalize() changes the first character and the rest
Python upper-cases (strictly, title-cases) the first character and converts the remaining ones to lowercase. If the string starts with a digit or a space, nothing at the start changes, but the rest is still lower-cased.
print("hello WORLD".capitalize())
print("3 APPLES".capitalize())
print(" leading space".capitalize())
print("".capitalize() == "")Capitalise only the first letter and keep the rest
When you must preserve the rest of the text, such as an acronym in the middle, build the result with slicing: upper-case text[:1] and add text[1:] unchanged. Using [:1] instead of [0] keeps it safe for empty strings.
text = "learn about NASA missions"
print(text.capitalize())
print(text[:1].upper() + text[1:])capitalize() compared with the other case methods
| Method | Result for "pYTHON is fun" | Notes |
|---|---|---|
capitalize() | 'Python is fun' | First character up, rest down |
title() | 'Python Is Fun' | Every word capitalised |
upper() | 'PYTHON IS FUN' | Everything up |
lower() | 'python is fun' | Everything down |
swapcase() | 'Python IS FUN' | Cases flipped |
s[:1].upper() + s[1:] | 'PYTHON is fun' | Rest unchanged |
Examples you can run
Press Run, then edit the code and run it again.
Capitalise a sentence typed in capitals
The first letter is upper-case and the rest lower-case.
message = "WELCOME TO THE SHOP"
print(message.capitalize())Expected outputWelcome to the shop
Capitalise a list of names
Combine with a list comprehension.
names = ["asha", "RAVI", "mEENA"]
print([n.capitalize() for n in names])Expected output['Asha', 'Ravi', 'Meena']
Keep the rest of the text unchanged
Slice the first character and leave the remainder alone.
title = "iPhone sales rose"
print(title.capitalize())
print(title[:1].upper() + title[1:])Expected outputIphone sales rose
IPhone sales rose
Common mistakes
Expecting capitalize() to keep other capitals
✗ Wrong
print("my iPhone".capitalize())✓ Right
text = "my iPhone"
print(text[:1].upper() + text[1:])Use slicing to change only the first letter.
Using capitalize() to capitalise every word
✗ Wrong
print("learn python fast".capitalize())✓ Right
print("learn python fast".title())Use title() or capitalise each word after splitting.
Ignoring the return value
✗ Wrong
word = "python"
word.capitalize()
print(word)✓ Right
word = "python"
word = word.capitalize()
print(word)Assign the result back.
Python String capitalize() Method: questions people ask
How do I capitalise the first letter of a string in Python?
Use text.capitalize(), but remember it lower-cases the rest. To keep the rest as it is, use text[:1].upper() + text[1:].
What is the difference between capitalize() and title()?
capitalize() changes only the first character of the whole string, while title() capitalises the first letter of every word.
Does capitalize() lower-case the rest of the string?
Yes. Every character after the first is converted to lowercase.
Keep learning
Step-by-step lessons:
Python StringsPython String MethodsPython Slicing
Related:
Python String title() 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 →