This guide explains str.capitalize Method: Usage and Examples through a focused example and practical reasoning. No prior knowledge of this specific operation is required.
Understanding the Concept
str.capitalize() returns a new string with its first character capitalized and the remaining characters lowercased. It does not change the original string. The result is not a reliable title-casing tool for personal names.
When You Would Use This
Make a short label look consistent in a simple confirmation message.
Step-by-Step Example
status_message = "pYTHON IS READY"
formatted_message = status_message.capitalize()
print(formatted_message)
print(status_message)
How the Example Works
The first line prints Python is ready and the second prints the unchanged original. capitalize() applies case conversion across the rest of the text, not only the first letter.
Example Output
Python is ready
pYTHON IS READY
Try It Yourself
Try "hELLO aLEX" and compare .capitalize() with .title().
Common Beginner Mistakes
- Expecting every word to start with a capital letter.
- Assuming the original string is modified in place.
- Using capitalization rules to rewrite proper nouns without checking the result.
Key Takeaways
str.capitalize()returns a new string with its first character capitalized and the remaining characters lowercased.- The first line prints
Python is readyand the second prints the unchanged original. - Avoid this mistake: Expecting every word to start with a capital letter.