Python .capitalize()
The Python .capitalize() method is a built-in string method that converts the first character of a string to uppercase and all subsequent characters to lowercase. It is particularly useful for presenting data, handling user input, or preparing text for display.
Python .capitalize() Syntax
str.capitalize()
Parameters:
The Python .capitalize() method does not take any parameters.
Return value:
Returns a new string with the changes applied.
Example 1: Handling Uppercase Sentences Using Python .capitalize()
This example uses Python .capitalize() on an uppercase sentence:
print("WELCOME TO CODECADEMY DOCS!".capitalize())
This will result in:
Welcome to codecademy docs!
Example 2: Handling Lowercase Sentences Using Python .capitalize()
This example uses Python .capitalize() on a lowercase sentence:
text = "welcome to new york city"print(text.capitalize())
This will result in:
Welcome to new york city
Codebyte Example: Handling Mixed-Case Sentences Using Python .capitalize()
This codebyte example uses Python .capitalize() on a mixed-case sentence:
Frequently Asked Questions
1. What is Python .capitalize()?
The Python .capitalize() method is a built-in string method that converts the first character of a string to uppercase and all subsequent characters to lowercase.
2. How to capitalize all text in Python?
If you want to convert all characters in a string to uppercase in Python, you should use the .upper() method instead of .capitalize():
text = "hello world"upper_text = text.upper()print(upper_text)
Here is the output:
HELLO WORLD
3. Does Python .capitalize() modify the original string?
No, Python .capitalize() does not modify the original string. Strings in Python are immutable, which means methods like .capitalize() return a new string and leave the original unchanged.
All contributors
Sriparno08
CaupolicanDiaz
christian.dinh
anli2- Anonymous contributor
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Learn to analyze and visualize data using Python and statistics.
- Includes 8 Courses
- With Certificate
- Intermediate.13 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours