You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.'
4
4
Subjects:
5
-
- 'Data Science'
6
5
- 'Computer Science'
6
+
- 'Data Science'
7
7
Tags:
8
-
- 'Strings'
9
-
- 'Methods'
8
+
- 'Characters'
10
9
- 'Functions'
10
+
- 'Methods'
11
+
- 'Strings'
11
12
CatalogContent:
12
13
- 'learn-python-3'
13
14
- 'paths/analyze-data-with-python'
14
15
---
15
16
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.
17
18
18
-
## Syntax
19
+
## Python `.capitalize()`Syntax
19
20
20
21
```pseudo
21
-
"string".capitalize()
22
+
str.capitalize()
22
23
```
23
24
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:**
26
30
27
-
## Example
31
+
Returns a new string with the changes applied.
28
32
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:
30
36
31
37
```py
32
38
print("WELCOME TO CODECADEMY DOCS!".capitalize())
@@ -38,11 +44,58 @@ This will result in:
38
44
Welcome to codecademy docs!
39
45
```
40
46
41
-
## Codebyte Example
47
+
## Example 2: Handling Lowercase Sentences Using Python `.capitalize()`
42
48
43
-
The following example is runnable and uses the`.capitalize()`method:
49
+
This example uses Python`.capitalize()`on a lowercase sentence:
44
50
45
-
```codebyte/python
51
+
```py
46
52
text ="welcome to new york city"
53
+
47
54
print(text.capitalize())
48
55
```
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