From d3b21c1f38ec5def9ad65ef6db86b35c68be9e46 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Mon, 16 Jun 2025 12:57:14 +0530 Subject: [PATCH 1/2] [Edit] Python: .replace() --- .../concepts/strings/terms/replace/replace.md | 126 ++++++++++-------- 1 file changed, 74 insertions(+), 52 deletions(-) diff --git a/content/python/concepts/strings/terms/replace/replace.md b/content/python/concepts/strings/terms/replace/replace.md index dca0022a1cb..72896427146 100644 --- a/content/python/concepts/strings/terms/replace/replace.md +++ b/content/python/concepts/strings/terms/replace/replace.md @@ -1,102 +1,124 @@ --- Title: '.replace()' -Description: 'Replace a specific substring with another substring.' +Description: 'Replaces a given substring with another substring in a string.' Subjects: - - 'Data Science' - 'Computer Science' + - 'Data Science' Tags: - - 'Strings' + - 'Functions' - 'Methods' + - 'Strings' + - 'Values' CatalogContent: - 'learn-python-3' - 'paths/data-science' - - 'paths/computer-science' --- -Replace a specific substring with another substring. +In Python, the **`.replace()`** method replaces a given [substring](https://www.codecademy.com/resources/docs/python/substrings) with another substring in a string. This method is useful for text processing, data cleaning, and formatting tasks. ## Syntax -```py +```pseudo string.replace(old, new, count) ``` -The `.replace()` string method takes in three parameters: +**Parameters:** -- `old`: The substring to search for. (Required) -- `new`: The substring to replace. (Required) -- `count`: A number specifying how many occurrences of the old value to replace. Default is all occurrences. +- `old`: The substring to be replaced. +- `new`: The substring to replace with. +- `count`: Specifies the number of occurrences of the given substring to be replaced. If not specified, all occurrences of the given substring is replaced. -## Example 1 +**Return value:** -`.replace()` can be called either directly on a string: +The `.replace()` method returns another string with the given substring replaced. -```py -welcome = "Hello, world!".replace("world", "Codecademy") - -print(welcome) -# Output: Hello, Codecademy! -``` +## Example 1: Basic Usage of `.replace()` -Or on a variable assigned to a string: +This example uses the `.replace()` method to replace "world" with "Codecademy" in the `welcome` string: ```py welcome = "Hello, world!" + welcome = welcome.replace("world", "Codecademy") print(welcome) -# Output: Hello, Codecademy! ``` -Because `replace()` is a method, it returns a new string and does not modify the original string. Therefore: +Here is the output: -```py -var = "x" -var.replace("x", "y") -print(var) -# Output: x +```shell +Hello, Codecademy! ``` -By default, `replace()` will replace all occurrences in the string. However, you can add an integer to specify how many strings should be replaced. +## Example 2: Using `.replace()` with `count` + +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: ```py -var = "I like cats and cats like me" -var = var.replace("like", "LOVE") -print(var) -# Output: "I LOVE cats and cats LOVE me" +var = "I like cats and cats like me." + +var = var.replace("like", "love", 1) -var = "I like cats and cats like me" -var = var.replace("like", "LOVE", 1) print(var) -# Output "I LOVE cats and cats like me" ``` -## Examples +Here is the output: + +```shell +I love cats and cats like me. +``` + +## Codebyte Example: Removing Substrings Using `.replace()` -The `replace()` method can be used to remove sections of a string entirely: +This codebyte example uses the `.replace()` method to remove "elephant " from the `myVar` string: -```codebyte/py -myString = "I am a sentence with an extra elephant word." -newString = myString.replace("elephant ", "") -print(newString) +```codebyte/python +myVar = "I am a sentence with an extra elephant word." + +newVar = myVar.replace("elephant ", "") + +print(newVar) ``` -It can also be called multiple times on the same string: +## Frequently Asked Questions + +### 1. Is `.replace()` case-sensitive? + +Yes, `.replace()` is case-sensitive. For example, `"Hello".replace("h", "J")` won’t work because "h" is not the same as "H". + +### 2. Can `.replace()` be used with variables? + +Yes. You can use variables for both old and new values in `.replace()`: + +```py +old = "apple" +new = "orange" + +fruit_text = "I like apple pie." -```codebyte/py -myString = "I am a sentence with an extra elephant word." -newString = myString.replace("elephant ", "").replace("with", "without") -print(newString) +print(fruit_text.replace(old, new)) ``` -If there are many words that need to be removed, consider using a `for` loop: +Here is the output: -```codebyte/py -wordsToReplace = ["rocks! ", "He ", "never ", "and ", "loves "] -mySentence = "My cat rocks! He never bites and loves me." +```shell +I like orange pie. +``` + +### 3. Can you replace substrings in a loop using `.replace()`? + +Yes, and you can also chain `.replace()` calls for multiple replacements: + +```py +text = "I love red and blue." + +text = text.replace("red", "green").replace("blue", "yellow") + +print(text) +``` -for word in wordsToReplace: - mySentence = mySentence.replace(word, "") +Here is the output: -print(mySentence) +```shell +I love green and yellow. ``` From 6a0a114b22877a0d6f71b0105c37f3ad0c918444 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 18 Jun 2025 19:01:22 +0530 Subject: [PATCH 2/2] minor content tweaks --- content/python/concepts/strings/terms/replace/replace.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/python/concepts/strings/terms/replace/replace.md b/content/python/concepts/strings/terms/replace/replace.md index 72896427146..9ce43386029 100644 --- a/content/python/concepts/strings/terms/replace/replace.md +++ b/content/python/concepts/strings/terms/replace/replace.md @@ -14,7 +14,7 @@ CatalogContent: - 'paths/data-science' --- -In Python, the **`.replace()`** method replaces a given [substring](https://www.codecademy.com/resources/docs/python/substrings) with another substring in a string. This method is useful for text processing, data cleaning, and formatting tasks. +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. ## Syntax @@ -26,7 +26,7 @@ string.replace(old, new, count) - `old`: The substring to be replaced. - `new`: The substring to replace with. -- `count`: Specifies the number of occurrences of the given substring to be replaced. If not specified, all occurrences of the given substring is replaced. +- `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. **Return value:**