Signup/Sign In
LAST UPDATED: JUNE 4, 2020

Python String replace()

If anyone wants to replace a string or phrase with another string or phrase or if you want to replace some old value with some new values in that case Python String replace() is used.

  • Python String replace() is an inbuilt function.

  • It is used to replace a specified phrase or string with another phrase or string and returns the result after replacement.

  • This method does not modify the original string.

  • This method basically returns a copy of a string where all occurrences of its substring are replaced by some other substring.

  • In layman terms it replaces the old substring with the new substring and returns the copy of the string; one thing to remember is that it does not modify the original string.

  • This method comes under the category of <class 'str'>

Python String replace(): Syntax

Below we have a basic syntax of String replace() in Python:

string.replace(oldvalue, newvalue, count)

Python String replace(): Parameters

Given below is a description of Parameters of replace() as there are three parameters:

  • oldvalue

It is a mandatory attribute used to specify the substring that is needed to be searched.

  • newvalue

It is a mandatory attribute that is used to specify the value we need to replace.

  • count

It is an optional parameter and is a number that is used to specify how many occurrences of the old value you want to replace.

Python String replace(): Basic Example

Below we have an example to show the working of String replace() function:

str = 'ABC Movie is  a great movie'

replacedString = line.replace('ABC', 'titanic')

print(replacedString)

In the above example, we have not used the count parameter. The value of the original string remains unchanged; The output for the same is given below:


titanic movie is a great movie

Python String replace(): Using count Parameter

When we use count Parameter, then it is used to specify how many occurrences of the old value you want to replace. Let us see the example for the same:

str = 'Every DC Movies are great Movies!! Really great Movies'

replacedString = str.replace('Movies', 'Cartoons', 2)

print(replacedString)

The output for the same is given below:


Every DC Cartoons are great Cartoons!! Really great Movies

Python String replace() with RegExp

One important thing here is that in Python str.replace() is unable to recognize now what if you want to make changes in regular expressions or special characters?

The answer to the above question is that you need to import the re module and after that, you need to use the sub() method. Let us see this with an example:

import re

data = "The pollution in the air"
res = re.sub("\s", "||", data)
print(res)

The output for the same is given below:


The||Pollution||in||the||air

Python String replace(): backslashes to slashes

In the example given below, we are trying to replace the backslashes in a string with slashes.


data = "images\\abc.jpg"
re_data = data.replace("\\", "/")
print(re_data)

The output for the same will be:


images/abc.jpg

Time For Live Example!

Let us see the live example of replace() for a clear understanding of it and it is given below:

Summary

In this tutorial, we learned the replace() method of strings in Python which is used to replace a string with another string. We also saw its syntax and parameters with a few examples. We had also replaced backslashes to slashes and at the end a live example for the same.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.