---
title: "str.replace"
space: "Student Handbook"
url: "https://portal.cs17.org/student-handbook/python-manual/strings/replace"
updated: "2026-10-05"
---

Returns a copy of a string with one piece of text swapped for another.

## Synopsis

```python
text.replace(old, new)
```

## Description

`replace` changes every place `old` appears. It is case-sensitive.

## Parameters

| Name | Meaning |
| --- | --- |
| `old` | The text to find. |
| `new` | The text to put in its place. |

## Return value

A new `str`.

## Example

```python
print("I like cats".replace("cats", "dogs"))
print("a b c".replace(" ", ""))
print("banana".replace("a", "o"))
```

It prints:

```text
I like dogs
abc
bonono
```

## Common mistake

```python
print("Hello".replace("h", "J"))
```

It prints:

```text
Hello
```

Nothing changed, because `"h"` and `"H"` are different characters.

## See also

[str.strip](/student-handbook/python-manual/strings/strip), [str.lower](/student-handbook/python-manual/strings/lower)

[str.replace in the Python documentation](https://docs.python.org/3/library/stdtypes.html#str.replace)
