Python replace() String Method with Examples

How to use the Python replace() String Method

In this tutorial, you'll learn how to use the replace() string method in Python. The replace() method returns a copy of the string with all occurrences of old substring replaced by a new substring.

Python replace() Syntax

The following illustrates the syntax of the replace() string method:

string.replace(old_substring, new_substring [, count])

Method Parameters

  • old_substring: Required. The old substring you want to replace.
  • new_substring: Required. The new substring which will replace the old substring.
  • count: Optional. The replace() method replaces all occurrences of old_substring if omitted.

Examples

Example 1: Replace all Occurrences

The following code replaces all occurrences of Python Programming with Ruby Programming:

description = "Python Programming, C Programming, Python Programming, C++ Programming, C# Programming, Python Programming, Pascal Programming, Python Programming";
str = description.replace("Python Programming", "Ruby Programming")
print(str)

Output:

Ruby Programming, C Programming, Ruby Programming, C++ Programming, C# Programming, Ruby Programming, Pascal Programming, Ruby Programming

Example 2: Replace Only a Specified Number of Occurrences

The following example illustrates an example of how to replace only the first two occurrences of Python Programming with Ruby Programming:

description = "C Programming, Python Programming, C++ Programming, Python Programming, C# Programming, Python Programming, Pascal Programming, Python Programming";
str = description.replace("Python Programming", "Ruby Programming")
print(str)

Output:

C Programming, Ruby Programming, C++ Programming, Ruby Programming, C# Programming, Python Programming, Pascal Programming, Python Programming

Example 3: Replace all Occurrences Starting at a Specified Position

You cannot use the replace() method to replace occurrences of an old substring with a new substring, starting at a specified position in the string. However, you can achieve this by writing your program. 

Let's say you want to replace the old substring starting at index 60 of a given string.

First, what you need to do is to split the string into two substrings (str1and str2). The str1is the substring, starting at 0 to 60, and str2is the substring, starting at 60 to the end of the string.

Second, replace the old substring with a new substring in str2, and then assign returned substring to a variable named str3.

Third, join str1and str3, and now you'll get the result.

Here is an example:

description = "C Programming, Python Programming, C++ Programming, Python Programming, C# Programming, Python Programming, Pascal Programming, Python Programming";
len = len(description) #get the length of the string
str1 = description[0:60] #get the substring from index 0 to 60
str2 = description[60:len] #get the subtring from index 60 to the end of the string
str3 = str2.replace("Python Programming", "Ruby Programming") #replace Python Programming with Ruby Programming
str = str1 + str3
print (str)

Output:

C Programming, Python Programming, C++ Programming, Python Programming, C# Programming, Ruby Programming, Pascal Programming, Ruby Programming

Example 4: Replace from the End of a String

Replace a substring from the end of a string (replaces only two occurrences of Python with JavaScript):

string = "Python, Java, Python, Java, C++, C#, Visual Basic, Ruby, Python, Python, C#"
reversed_string = string[::-1]
old_substring = "Python"
new_substring = "JavaScript"
reversed_old_substring = old_substring[::-1]
reversed_new_substring = new_substring[::-1]
str = reversed_string.replace(reversed_old_substring, reversed_new_substring, 2)
print (str[::-1])

Output:

Python, Java, Python, Java, C++, C#, Visual Basic, Ruby, JavaScript, JavaScript, C#

In this tutorial, you've learned how to use the Python replace() string method to replace all occurrences of an old substring within a given string with a new substring.


See also:
Python return Statement with Examples
Python List Methods and Functions with Examples
Python abs() – Absolute Value in Python
Python Factorial – Find the Factorial of a Number
Python min() – Find the Smallest Value in Python

Leave a Comment