Latest Code Tutorials

Python String Replace Example | Python string replace()


Python string replace method returns the copy of the string where all occurrences of the substring are replaced with another substring. Python String replace() method replaces the specified phrase with another specified phrase.

All occurrences of the particular phrase will be replaced if nothing is specified on the function argument. The replace() function returns the copy of the string in which the values of the old string have been replaced with a new value.

Python String Replace

Content Overview

In Python, everything is an object, and the string is an object too.

Python strings can be created simply by enclosing characters in a double quote.

Often you will have the string (str object), where you want to modify the contents by replacing one text with another.

Luckily for us, Python’s string module comes with the replace() method. Python replace() method is part of the string module and can be called either from an str object or from the string module alone.

The replace() method returns the copy of the string where the old substring is replaced with the new substring — one thing to keep in mind that the original string is unchanged.

If an old substring is not found, it returns the copy of an original string.

The replace() is a method of <class ‘str’> in python3.

The syntax of the Python string replace function is the following.

string.replace(oldvalue, newvalue, count)

The oldvalue parameter is required, and it is the value we need to search for.

The newvalue parameter is required, and it is the value we need to replace.

The count parameter is optional, and it is a number specifying how many occurrences of the old value you want to replace.

Let’s see the example.


line = 'Every DC Movies are great movies'

replacedString = line.replace('DC', 'Marvel')


In the above example, we are replacing the DC substring with the Marvel.

See the output below.

Python String Replace Example

Here one thing is to notice that the original string is unchanged.

Let’s see the example where we will specify how many times you need to change the occurrences.


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

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


In the above example, we are changing only two times from Movies to Cartoons.

Python String Replace Example | Replace() Method Tutorial

#Python String replace() example

See the below examples.


str = 'cold, cold heart'
replaced_str =  str.replace('o', 'e')
print ('Original string:', str)
print ('Replaced string:', replaced_str)

str = 'let it be, let it be, let it be'
print(str.replace('let', 'so', 0))

See the output.

More Example on Python String replace()

#Python replace() with user input

Let’s see the following code example where a user input string replaces the string.

inputStr = input('Please provide input string\n')
delimiter = input('Please provide delimiter\n')
delimiter_new = input('Please provide new delimiter\n')
newStr = inputStr.replace(delimiter, delimiter_new)
print('Replaced Data =', newStr)

See the output.

➜  pyt python3
Please provide input string
mike is good character from strangerthings
Please provide delimiter
Please provide new delimiter
Replaced Data = eleven is good character from strangerthings
➜  pyt

#Python string replace regex

Python str.replace() method does not recognize regular expressions.

If you want to use the regular expression in Python, then you need to import the re module and then use the method sub().

See the following code example.

import re

data = "The rain in Australia"
res = re.sub("\s", "||", data)

See the output.

➜  pyt python3
➜  pyt

#Python replace special characters in string

Now, again, you should use the regular expression to replace the special characters.

import re

singer = "cami!l!a cabel!!l@o i!s sen~!orita"
new_singer = re.sub('[^a-zA-Z0-9 \n\.]', '', singer)

See the output.

➜  pyt python3
camila cabello is senorita
➜  pyt

#Python replace backslashes to slashes

If you want to replace backslashes to slashes, then use the string.replace() method.

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

See the output.

➜  pyt python3
➜  pyt


Hopefully, the above article would have given you a fair idea of using the string replace() method in Python.

You can also read an article on replace() function on Python official docs.

Finally, Python String Replace Example is over.

Recommended Posts

Python string startswith()

Python string count()

Python string join()

Python string index()

Python string format()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.