|
1 | 1 | --- |
2 | 2 | Title: '.replace()' |
3 | | -Description: 'Replace a specific substring with another substring.' |
| 3 | +Description: 'Replaces a given substring with another substring in a string.' |
4 | 4 | Subjects: |
5 | | - - 'Data Science' |
6 | 5 | - 'Computer Science' |
| 6 | + - 'Data Science' |
7 | 7 | Tags: |
8 | | - - 'Strings' |
| 8 | + - 'Functions' |
9 | 9 | - 'Methods' |
| 10 | + - 'Strings' |
| 11 | + - 'Values' |
10 | 12 | CatalogContent: |
11 | 13 | - 'learn-python-3' |
12 | 14 | - 'paths/data-science' |
13 | | - - 'paths/computer-science' |
14 | 15 | --- |
15 | 16 |
|
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. |
17 | 18 |
|
18 | 19 | ## Syntax |
19 | 20 |
|
20 | | -```py |
| 21 | +```pseudo |
21 | 22 | string.replace(old, new, count) |
22 | 23 | ``` |
23 | 24 |
|
24 | | -The `.replace()` string method takes in three parameters: |
| 25 | +**Parameters:** |
25 | 26 |
|
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. |
29 | 30 |
|
30 | | -## Example 1 |
| 31 | +**Return value:** |
31 | 32 |
|
32 | | -`.replace()` can be called either directly on a string: |
| 33 | +The `.replace()` method returns another string with the given substring replaced. |
33 | 34 |
|
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()` |
40 | 36 |
|
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: |
42 | 38 |
|
43 | 39 | ```py |
44 | 40 | welcome = "Hello, world!" |
| 41 | + |
45 | 42 | welcome = welcome.replace("world", "Codecademy") |
46 | 43 |
|
47 | 44 | print(welcome) |
48 | | -# Output: Hello, Codecademy! |
49 | 45 | ``` |
50 | 46 |
|
51 | | -Because `replace()` is a method, it returns a new string and does not modify the original string. Therefore: |
| 47 | +Here is the output: |
52 | 48 |
|
53 | | -```py |
54 | | -var = "x" |
55 | | -var.replace("x", "y") |
56 | | -print(var) |
57 | | -# Output: x |
| 49 | +```shell |
| 50 | +Hello, Codecademy! |
58 | 51 | ``` |
59 | 52 |
|
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: |
61 | 56 |
|
62 | 57 | ```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) |
67 | 61 |
|
68 | | -var = "I like cats and cats like me" |
69 | | -var = var.replace("like", "LOVE", 1) |
70 | 62 | print(var) |
71 | | -# Output "I LOVE cats and cats like me" |
72 | 63 | ``` |
73 | 64 |
|
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()` |
75 | 72 |
|
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: |
77 | 74 |
|
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) |
82 | 81 | ``` |
83 | 82 |
|
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." |
85 | 98 |
|
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)) |
90 | 100 | ``` |
91 | 101 |
|
92 | | -If there are many words that need to be removed, consider using a `for` loop: |
| 102 | +Here is the output: |
93 | 103 |
|
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 | +``` |
97 | 119 |
|
98 | | -for word in wordsToReplace: |
99 | | - mySentence = mySentence.replace(word, "") |
| 120 | +Here is the output: |
100 | 121 |
|
101 | | -print(mySentence) |
| 122 | +```shell |
| 123 | +I love green and yellow. |
102 | 124 | ``` |
0 commit comments