Skip to content

Commit c750d87

Browse files
authored
[Edit] Python: .replace() (#7117)
* [Edit] Python: .replace() * minor content tweaks ---------
1 parent ad459ac commit c750d87

File tree

1 file changed

+74
-52
lines changed
  • content/python/concepts/strings/terms/replace

1 file changed

+74
-52
lines changed
Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,124 @@
11
---
22
Title: '.replace()'
3-
Description: 'Replace a specific substring with another substring.'
3+
Description: 'Replaces a given substring with another substring in a string.'
44
Subjects:
5-
- 'Data Science'
65
- 'Computer Science'
6+
- 'Data Science'
77
Tags:
8-
- 'Strings'
8+
- 'Functions'
99
- 'Methods'
10+
- 'Strings'
11+
- 'Values'
1012
CatalogContent:
1113
- 'learn-python-3'
1214
- 'paths/data-science'
13-
- 'paths/computer-science'
1415
---
1516

16-
Replace a specific substring with another substring.
17+
In Python, the **`.replace()`** method replaces all occurrences of a specified [substring](https://www.codecademy.com/resources/docs/python/substrings) with another substring in a string. It is commonly used for text processing, data cleaning, and formatting tasks.
1718

1819
## Syntax
1920

20-
```py
21+
```pseudo
2122
string.replace(old, new, count)
2223
```
2324

24-
The `.replace()` string method takes in three parameters:
25+
**Parameters:**
2526

26-
- `old`: The substring to search for. (Required)
27-
- `new`: The substring to replace. (Required)
28-
- `count`: A number specifying how many occurrences of the old value to replace. Default is all occurrences.
27+
- `old`: The substring to be replaced.
28+
- `new`: The substring to replace with.
29+
- `count` (optional): Specifies the number of occurrences of the given substring to be replaced. If not specified, all occurrences of the given substring is replaced.
2930

30-
## Example 1
31+
**Return value:**
3132

32-
`.replace()` can be called either directly on a string:
33+
The `.replace()` method returns another string with the given substring replaced.
3334

34-
```py
35-
welcome = "Hello, world!".replace("world", "Codecademy")
36-
37-
print(welcome)
38-
# Output: Hello, Codecademy!
39-
```
35+
## Example 1: Basic Usage of `.replace()`
4036

41-
Or on a variable assigned to a string:
37+
This example uses the `.replace()` method to replace "world" with "Codecademy" in the `welcome` string:
4238

4339
```py
4440
welcome = "Hello, world!"
41+
4542
welcome = welcome.replace("world", "Codecademy")
4643

4744
print(welcome)
48-
# Output: Hello, Codecademy!
4945
```
5046

51-
Because `replace()` is a method, it returns a new string and does not modify the original string. Therefore:
47+
Here is the output:
5248

53-
```py
54-
var = "x"
55-
var.replace("x", "y")
56-
print(var)
57-
# Output: x
49+
```shell
50+
Hello, Codecademy!
5851
```
5952

60-
By default, `replace()` will replace all occurrences in the string. However, you can add an integer to specify how many strings should be replaced.
53+
## Example 2: Using `.replace()` with `count`
54+
55+
This example uses the `.replace()` method with the `count` parameter to replace only the first occurrence (`count = 1`) of "like" with "love" in the `var` string:
6156

6257
```py
63-
var = "I like cats and cats like me"
64-
var = var.replace("like", "LOVE")
65-
print(var)
66-
# Output: "I LOVE cats and cats LOVE me"
58+
var = "I like cats and cats like me."
59+
60+
var = var.replace("like", "love", 1)
6761

68-
var = "I like cats and cats like me"
69-
var = var.replace("like", "LOVE", 1)
7062
print(var)
71-
# Output "I LOVE cats and cats like me"
7263
```
7364

74-
## Examples
65+
Here is the output:
66+
67+
```shell
68+
I love cats and cats like me.
69+
```
70+
71+
## Codebyte Example: Removing Substrings Using `.replace()`
7572

76-
The `replace()` method can be used to remove sections of a string entirely:
73+
This codebyte example uses the `.replace()` method to remove "elephant " from the `myVar` string:
7774

78-
```codebyte/py
79-
myString = "I am a sentence with an extra elephant word."
80-
newString = myString.replace("elephant ", "")
81-
print(newString)
75+
```codebyte/python
76+
myVar = "I am a sentence with an extra elephant word."
77+
78+
newVar = myVar.replace("elephant ", "")
79+
80+
print(newVar)
8281
```
8382

84-
It can also be called multiple times on the same string:
83+
## Frequently Asked Questions
84+
85+
### 1. Is `.replace()` case-sensitive?
86+
87+
Yes, `.replace()` is case-sensitive. For example, `"Hello".replace("h", "J")` won’t work because "h" is not the same as "H".
88+
89+
### 2. Can `.replace()` be used with variables?
90+
91+
Yes. You can use variables for both old and new values in `.replace()`:
92+
93+
```py
94+
old = "apple"
95+
new = "orange"
96+
97+
fruit_text = "I like apple pie."
8598

86-
```codebyte/py
87-
myString = "I am a sentence with an extra elephant word."
88-
newString = myString.replace("elephant ", "").replace("with", "without")
89-
print(newString)
99+
print(fruit_text.replace(old, new))
90100
```
91101

92-
If there are many words that need to be removed, consider using a `for` loop:
102+
Here is the output:
93103

94-
```codebyte/py
95-
wordsToReplace = ["rocks! ", "He ", "never ", "and ", "loves "]
96-
mySentence = "My cat rocks! He never bites and loves me."
104+
```shell
105+
I like orange pie.
106+
```
107+
108+
### 3. Can you replace substrings in a loop using `.replace()`?
109+
110+
Yes, and you can also chain `.replace()` calls for multiple replacements:
111+
112+
```py
113+
text = "I love red and blue."
114+
115+
text = text.replace("red", "green").replace("blue", "yellow")
116+
117+
print(text)
118+
```
97119

98-
for word in wordsToReplace:
99-
mySentence = mySentence.replace(word, "")
120+
Here is the output:
100121

101-
print(mySentence)
122+
```shell
123+
I love green and yellow.
102124
```

0 commit comments

Comments
 (0)