The str.capitalize() method returns a copy of the string with its first character converted to uppercase and all remaining characters converted to lowercase. It is commonly used for formatting sentence starters, user input names, and display headings.
Word-by-Word Capitalization: To capitalize the first letter of every word in a string, use str.title().
The capitalize() method takes no arguments and is invoked directly on a string:
new_string = str.capitalize()
| Parameter | Type | Required? | Description |
|---|---|---|---|
| (None) | — | No | The method takes no parameters. Passing any arguments raises a TypeError. |
Return Value: Returns a new string (str). Because Python strings are immutable, the original string remains completely unchanged.
When applied to all-lowercase strings, capitalize() converts the character at index 0 to uppercase:
msg = "welcome to plus2net"
print(msg.capitalize()) # Output: Welcome to plus2net
single_char = "a"
print(single_char.capitalize()) # Output: A
empty_str = ""
print(repr(empty_str.capitalize())) # Output: ''
Welcome to plus2net
A
''
A crucial behavior of str.capitalize() that often catches developers off guard is that every character after index 0 is converted to lowercase. It does not simply uppercase the first letter:
text1 = "HELLO WORLD"
print(text1.capitalize()) # Output: "Hello world"
text2 = "mCDONALD"
print(text2.capitalize()) # Output: "Mcdonald" (internal 'D' became 'd'!)
text3 = "iPAd Pro"
print(text3.capitalize()) # Output: "Ipad pro"
"uSA"), camelCase names, or mixed-case brand names, capitalize() will strip those internal capitals (turning "uSA" into "Usa").
If the first character cannot be converted to uppercase (such as numbers, punctuation, or whitespace), the first character remains unchanged, but subsequent uppercase characters are still forced to lowercase:
# 1. Leading number
s1 = "100 PYTHON tips"
print(s1.capitalize()) # Output: "100 python tips"
# 2. Leading symbol
s2 = "@PLUS2NET"
print(s2.capitalize()) # Output: "@plus2net"
# 3. Leading whitespace
s3 = " hello WORLD"
print(s3.capitalize()) # Output: " hello world" (first char was space!)
Tip: If your string might have leading whitespace, use s.strip().capitalize() to ensure the first actual letter is the one capitalized.
Python provides several case-transformation methods. Here is how they compare:
| Input String | capitalize() |
title() |
upper() |
|---|---|---|---|
"hello world" |
"Hello world" |
"Hello World" |
"HELLO WORLD" |
"HELLO WORLD" |
"Hello world" |
"Hello World" |
"HELLO WORLD" |
"2nd place winner" |
"2nd place winner" |
"2Nd Place Winner" |
"2ND PLACE WINNER" |
"python. programming." |
"Python. programming." |
"Python. Programming." |
"PYTHON. PROGRAMMING." |
If you only want to uppercase the first character while preserving the casing of all remaining characters, use string slicing instead of capitalize():
def uppercase_first_only(s):
if not s:
return s
return s[:1].upper() + s[1:]
brand = "mCDONALD"
acronym = "eBAY Inc"
print("With capitalize(): ", brand.capitalize()) # Mcdonald
print("With uppercase_first(): ", uppercase_first_only(brand)) # MCDONALD
print("With uppercase_first(): ", uppercase_first_only(acronym))# EBAY Inc
Because str.capitalize() only capitalizes the beginning of the entire string, multi-sentence paragraphs leave later sentences in lowercase (e.g. "first. second." → "First. second."). Let's construct a sentence capitalization utility and test it with unit assertions:
import re
def format_paragraph(text):
# Capitalize the first letter following start of string or punctuation (.!?) + whitespace
return re.sub(
r"(?:^|[.!?]\s+)([a-z])",
lambda m: m.group(0).upper(),
text
)
sample_text = "welcome to python. how are you? i am coding! it is great."
formatted_text = format_paragraph(sample_text)
# Unit Assertions
assert formatted_text == "Welcome to python. How are you? I am coding! It is great."
assert format_paragraph("") == ""
assert format_paragraph("single sentence.") == "Single sentence."
print("Original: ", sample_text)
print("Formatted:", formatted_text)
print("All paragraph formatting assertions passed successfully!")
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.