Python String replace()

Python String replace()

Python String replace() method replaces all the occurrences of an old value with a new value in the string.

Consider that given string is in x.

"apple cherry apple banana"

Then the return value of x.replace(“apple”, “fig”) is

"fig cherry fig banana"

In this tutorial, you will learn how to use string replace() method to replace all or only limited number of occurrences of an old substring with a new value.

Python String Replace

Syntax of replace() method

The syntax of string replace() method is:

str.replace(old, new [, count]) 

You can read the above expression as: In the string str, replace the occurrences of old substring value with a new value. And if count is specified, limit those replacements to a maximum number of count.

The search and replacement operation happens from left to right of the string.

Parameters

String replace() method takes two mandatory parameters, and one optional parameter.

ParameterDescription
oldRequired
This value is searched for, and replaced in the string.
This is also called search value, because we have to search for this value in the string.
newRequired
This value is used for replacing the old value in the string.
This is also called replacement value, because we have to replace the search value with this replacement value in the string.
countOptional
The maximum number of replacements that can be done.
The default value is the number of all occurrences of old value in the string.

Return value

The String replace() method returns a string value.

The original string on which we are calling the replace() method is not modified.

Examples

1. Replacing all occurrences of old string “apple” with “cherry” in Python

In the following example, we are give a string in my_string. In this string, we have to replace the old value "apple" with a new value "fig" using string replace() method.

Python Program

my_string = "apples are good. some apples are red."

replaced_string = my_string.replace("apple", "fig")

print(f"Original string : \"{my_string}\"")
print(f"Replaced string : \"{replaced_string}\"")
Run Code Copy

Output

Original string : "apples are good. some apples are red."
Replaced string : "figs are good. some figs are red."

2. Replacing old value in string for only a specific number of occurrences in Python

In this example, we have to replace the old string “apple” with new value “fig” only for a specific number of times, say 2.

We have to call replace() method on the original string, and pass the old value “apple” as first argument, new value “fig” as second argument, and the count value 2 as third argument.

Python Program

my_string = "apples are good. apples are red. apples are food. apples are ok."

replaced_string = my_string.replace("apple", "fig", 2)

print(f"Original String : \"{my_string}\"")
print(f"Replaced String : \"{replaced_string}\"")
Run Code Copy

Output

Original String : "apples are good. apples are red. apples are food. apples are ok."
Replaced String : "figs are good. figs are red. apples are food. apples are ok."

Only the first three occurrence of the old search string are replaced with the new replacement string.

3. Replacing character in Python

In the following example, we are give a string in my_string. In this string, we have to replace the character “p” with a new character value “m” using string replace() method.

Call replace() method on the string my_string, and pass the given search value “p” and replacement value “m” as arguments to the method.

Python Program

my_string = "apples pears peaches"

replaced_string = my_string.replace("p", "m")

print(f"Original string : \"{my_string}\"")
print(f"Replaced string : \"{replaced_string}\"")
Run Code Copy

Output

Original string : "apples pears peaches"
Replaced string : "ammles mears meaches"

Summary

In this tutorial of Python Examples, we learned about string replace() method, and how to use this string replace() method to replace the occurrences of a substring with another string, with the help of well detailed examples.

Related Tutorials

Code copied to clipboard successfully 👍