Skip to content

[Edit] Python: .replace() #7117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 74 additions & 52 deletions content/python/concepts/strings/terms/replace/replace.md
Original file line number Diff line number Diff line change
@@ -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 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

```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` (optional): 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.
```