In this article, we will learn to use string class’s replace() function to replace sub-strings in a string in Python.

In Python, the String class (Str) provides a method replace() to substitute the sub-strings in a string. But as strings are immutable in Python, this function returns a copy of the calling string object with the replaced content.

Syntax of the replace() function

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

Parameters:

  • old: The sub-string that needs to be replaced in the string.
  • new: The sub-string that will be used as a replacement by the replace() function.
  • count: The optional parameter of int type.
    • The number of times the old sub-string needs to be replaced.
      • If not provided, then replace() function will replace all the occurrences of old sub-string with new sub-string.
      • If provided, then replace() function will replace the only specified number of occurrences of old sub-string with the new sub-string.

Returns:

As strings are immutable in Python, i.e., we cannot change its contents. Therefore replace() function returns a copy of the string with the replaced content. If the replace() function does not find the sub-string in the original string, it returns a copy of the original string.

Let’s understand this with some examples,

Python: string replace() examples

Python: Replace all occurrences of a substring in a string

To replace all the occurrences of a substring in a string in Python, we will pass the sub-string and replacement string as arguments to the replace() function i.e.

sample_str = "This is a sample string, where is need to be replaced."

sample_str = sample_str.replace('is', 'ZZZ')
print(sample_str)

Output:

ThZZZ ZZZ a sample string, where ZZZ need to be replaced.

replace() function returned a copy of the original string but with modified content. It replaced all the three occurrences of ‘is’ sub-string with ‘ZZZ’ in the copied string.

We assigned the new string object returned by the replace() method to the same variable sample_str. It gave an effect that we have replaced the contents in the original string.

Python: Replace the first two occurrences of a substring in a string

Unlike the previous example, if we want to replace only the first two occurrences of a substring in a string, then we need to pass the count argument as 2 in the replace() function,

org_string = "This is a sample string, where is need to be replaced."

sample_str = sample_str.replace('is', 'ZZZ', 2)
print(sample_str)

Output:

ThZZZ ZZZ a sample string, where ZZZ need to be replaced.

replace() function returned a copy of the original string but with modified content. It replaced only the first two occurrences of ‘is’ sub-string with ‘ZZZ’ in the copied string.

The new string object returned by the replace() function was assigned to the same variable sample_str. It gave an effect that we have replaced the contents in the original string.

Python: Replace the first occurrence of a sub-string with a character in a string

If we want to replace only the first occurrences of a substring in a string with another character or sub-string, then we need to pass the count argument as 1 in the replace() function,

sample_str = "This is a sample string, where is need to be replaced."

# python string replace first occurrence only
sample_str = org_string.replace('is', 'X', 1)
print(sample_str)

Output:

ThX is a sample string, where is need to be replaced.

replace() function returned a copy of the original string but with modified content. It replaced the first occurrence of ‘is’ sub-string with ‘ZZZ’ in the copied string.

The new string object returned by the replace() function was assigned to the same variable sample_str. It gave an effect that we have replaced the contents in the original string.

Summary

As strings are immutable in Python, we can not modify its content, but we can use the replace() method to create a new string with the replaced substrings.

Join a list of 2000+ Programmers for latest Tips & Tutorials