Python String Replace – With Examples

In python, the string replace function, replace(), is used to replace the occurrences of a substring by a different substring. It is an inbuilt string function that returns a copy of the string where occurrences of the original substring have been replaced by the new substring.

s = "to be or not to be"
print(s.replace("be", "code"))

Output:

to code or not to code

  • Syntax
  • Examples

The following is the syntax to use the python string replace function, replace():

str.replace(old, new, count)

Parameters:

  • old: The old substring to be replaced.
  • new: The new substring you want to replace the old substring with.
  • count (optional): The number of times you want to replace the occurrence of the old substring with the new one. If it’s not provided, all the occurrences of the old substring are replaced by the new substring.

Returns:

A copy of the original string with occurrences of the old substring replaced by the new substring.

Note: If the value passed for the old parameter is not found, a copy of the original string without any change is returned.

Example 1: Replace all the occurrences of underscores _ with a single space ' '.

s = "to_be_or_not_to_be"
print("Original string: ", s)
# replace _ with a ' ' 
replaced_s = s.replace("_", " ") 
print("Replaced sring: ", replaced_s)

Output:

Original string:  to_be_or_not_to_be
Replaced String:  to be or not to be

In the above example, all the occurrences of the underscore symbol, _ , have been replaced by a single space, ' ', in the returned string.

Example 2: When the substring to be replaced is not present

s = "to be or not to be"
print("Original string: ", s)
replaced_s = s.replace("was", "code") 
print("Replaced string: ", replaced_s)

Output:

Original string:  to be or not to be
Replaced string:  to be or not to be

In the above example, since the value passed for the old parameter, was is not present in the string, a copy of the original string without any replacement is returned.

Example 3: Replacing only a fixed number of occurrences.

s = "to be or not to be"
print("Original string: ", s)
replaced_s = s.replace("be", "code", 1) 
print("Replaced string: ", replaced_s)

Output:

Original string:  to be or not to be
Replaced string:  to code or not to be

In the above example, we pass a third argument to the replace function. This argument controls the number of replacements to be made. Since we passed 1, only the first occurrence (from left) of the old substring be was replaced by the new substring code and all the other occurrences were unchanged in the returned string.

For more on python string replace function, refer the python docs.


In this tutorial, we covered how to replace occurrences of a substring in python using the replace() function. If you found this article useful do give it a share! For more such articles subscribe to us.

If you’re a beginner looking to start your data science journey and learn python, check out our Python for Data Science Series.