Python capitalize() Method

Definition
In Python, capitalize() returns a new string with the first character uppercased and the rest lowercased. The original string does not change.
It takes no arguments. Prefer title() when every word should start with a capital letter.
Syntax
Call capitalize on the string. Start with this line.
result = text.capitalize()
| Call | What it does |
|---|
"python is fun".capitalize() | Returns "Python is fun". |
"123ABC".capitalize() | Returns "123abc" (leading digits stay). |
Strings are immutable. Always assign or print the return value.
Library declarations
Parameters
This method has no parameters.
Return Value
| Return Value | Description |
|---|
| str | A new string with sentence-style casing. |
Python Examples
Example 1 — Basic capitalize()
Turn a lowercase phrase into sentence case and prove the original stays put.
original = "python is fun"
result = original.capitalize()
print("Original:", original)
print("Capitalized:", result)
Original: python is fun
Capitalized: Python is fun
How It Works
- Store the text.
original is all lowercase. - Call capitalize. Returns a new string into
result. - First letter rises.
p becomes P; the rest stay lowercase words. - Original untouched. Immutability keeps
original as "python is fun".
Example 2 — Strip, Then Capitalize
Clean messy input before casing—spaces first, then the first letter.
raw_name = " mari "
clean_name = raw_name.strip().capitalize()
print(clean_name)
How It Works
- Start messy. Extra spaces surround the name.
- strip() first. Removes leading/trailing whitespace →
"mari". - capitalize() next. First letter becomes
M. - Chain reads left to right. Each method returns a new string for the next call.
Example 3 — capitalize() vs title()
The classic beginner mix-up: one first letter vs every word.
name = "john doe"
print("capitalize():", name.capitalize())
print("title(): ", name.title())
capitalize(): John doe
title(): John Doe
How It Works
- Same input. Both methods start from
"john doe". - capitalize(). Only index 0 rises →
"John doe". - title(). Each word’s first letter rises →
"John Doe". - Pick by goal. Sentences →
capitalize; names/headings → title.
Frequently Asked Questions
It returns a new string with the first character uppercased and the rest lowercased.
No. Python strings are immutable. It always returns a new string.
capitalize() affects only the first character of the whole string. title() capitalizes each word.
It returns an empty string. No error is raised.
Remaining letters are still lowercased. For example, "123ABC".capitalize() returns "123abc".
Next: casefold()
Learn case-insensitive matching with Unicode-aware folding.
casefold() →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful