Skip to content

Commit 32cdac3

Browse files
authored
[Edit] Python: .capitalize() (#7608)
1 parent 80ed186 commit 32cdac3

File tree

1 file changed

+67
-14
lines changed

1 file changed

+67
-14
lines changed
Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
---
22
Title: '.capitalize()'
3-
Description: 'Takes in a string, and returns a copy of the string in capital case.'
3+
Description: 'Converts the first character of a string to uppercase and all subsequent characters to lowercase.'
44
Subjects:
5-
- 'Data Science'
65
- 'Computer Science'
6+
- 'Data Science'
77
Tags:
8-
- 'Strings'
9-
- 'Methods'
8+
- 'Characters'
109
- 'Functions'
10+
- 'Methods'
11+
- 'Strings'
1112
CatalogContent:
1213
- 'learn-python-3'
1314
- 'paths/analyze-data-with-python'
1415
---
1516

16-
The **`.capitalize()`** method takes in a string, and returns a copy of the string with the first character in upper case, and the remaining characters in lower case.
17+
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.
1718

18-
## Syntax
19+
## Python `.capitalize()` Syntax
1920

2021
```pseudo
21-
"string".capitalize()
22+
str.capitalize()
2223
```
2324

24-
- This method does not have any parameters.
25-
- It does not modify the original string, it returns a copy of the string with the applicable case changes.
25+
**Parameters:**
26+
27+
The Python `.capitalize()` method does not take any parameters.
28+
29+
**Return value:**
2630

27-
## Example
31+
Returns a new string with the changes applied.
2832

29-
The following example applies `.capitalize()` to a string in which all the characters are uppercase:
33+
## Example 1: Handling Uppercase Sentences Using Python `.capitalize()`
34+
35+
This example uses Python `.capitalize()` on an uppercase sentence:
3036

3137
```py
3238
print("WELCOME TO CODECADEMY DOCS!".capitalize())
@@ -38,11 +44,58 @@ This will result in:
3844
Welcome to codecademy docs!
3945
```
4046

41-
## Codebyte Example
47+
## Example 2: Handling Lowercase Sentences Using Python `.capitalize()`
4248

43-
The following example is runnable and uses the `.capitalize()` method:
49+
This example uses Python `.capitalize()` on a lowercase sentence:
4450

45-
```codebyte/python
51+
```py
4652
text = "welcome to new york city"
53+
4754
print(text.capitalize())
4855
```
56+
57+
This will result in:
58+
59+
```shell
60+
Welcome to new york city
61+
```
62+
63+
## Codebyte Example: Handling Mixed-Case Sentences Using Python `.capitalize()`
64+
65+
This codebyte example uses Python `.capitalize()` on a mixed-case sentence:
66+
67+
```codebyte/python
68+
text = "Welcome to Codecademy Docs!"
69+
70+
capitalized_text = text.capitalize()
71+
72+
print(capitalized_text)
73+
```
74+
75+
## Frequently Asked Questions
76+
77+
### 1. What is Python `.capitalize()`?
78+
79+
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.
80+
81+
### 2. How to capitalize all text in Python?
82+
83+
If you want to convert all characters in a string to uppercase in Python, you should use the `.upper()` method instead of `.capitalize()`:
84+
85+
```py
86+
text = "hello world"
87+
88+
upper_text = text.upper()
89+
90+
print(upper_text)
91+
```
92+
93+
Here is the output:
94+
95+
```shell
96+
HELLO WORLD
97+
```
98+
99+
### 3. Does Python `.capitalize()` modify the original string?
100+
101+
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.

0 commit comments

Comments
 (0)