How to replace all occurrences of a string with another string in Python?


A string is a group of characters that may be used to represent a single word or an entire phrase. In Python strings not require explicit declaration and may be defined with or without a specifier therefore, it is easy to use them.

Python has various built in functions and methods for manipulating and accessing strings. Because everything in Python is an object, a string is an object of the String class, which has several methods.

In this article, we are going to focus on replacing all occurrences of a string with another string in python.

Using the replace() method

The replace() method of string class accepts a string value as input and returns the modified string as output. It has 2 mandatory parameters and 1 optional parameter. Following is the syntax of this method.

string.replace(oldvalue, newvalue, count)

where,

  • Old value − The substring that you want to replace.

  • New value − This represents the substring with which you want to replace.

  • Count − This is an optional parameter; it is used to specify the number of old values you want to replace with new values.

Example 1

In the program given below, we are taking an input string and by using the replace method we are replacing the letter “t” with “d”.

str1 = "Welcome to tutorialspoint" print("The given string is") print(str1) print("After replacing t with d") print(str1.replace("t","d"))

Output

The output of the above program is,

The given string is
Welcome to tutorialspoint
After replacing t with d
Welcome do dudorialspoind

Example 2

In the program given below, we are taking the same input string and we are replacing the letter “t” with “d” using the replace() method, but in this example we are taking the count parameter as 2. So only 2 appearances of t are converted.

str1 = "Welcome to tutorialspoint" print("The given string is") print(str1) print("After replacing t with d for 2 times") print(str1.replace("t","d",2))

Output

The output of the above program is,

The given string is
Welcome to tutorialspoint
After replacing t with d for 2 times
Welcome do dutorialspoint

Using the regular expressions

We can also use Python regular expressions to replace all occurrences of a string with another string. The sub() method of python re replaces an existing letter in the given string with a new letter. Following is the syntax of this method −

re.sub(old, new, string);
  • Old − The sub string that you want to replace.

  • New − The new sub string with which you want to replace.

  • String − The source string.

Example

In the example given below, we are using the sub method of re library for replacing the letter “t” with “d”.

import re str1 = "Welcome to tutorialspoint" print("The given string is") print(str1) print("After replacing t with d ") print(re.sub("t","d",str1))

Output

The output of the above given program is,

The given string is
Welcome to tutorialspoint
After replacing t with d
Welcome do dudorialspoind

Traversing through each character

Another approach is the brute force approach where you traverse each character of a particular string and check it with the character you want to replace, if it matches then replace that character else move forward.

Example

In the example given below, we are iterating over the string and matching each character and replacing them.

str1= "Welcome to tutorialspoint" new_str = '' for i in str1: if(i == 't'): new_str += 'd' else: new_str += i print("The original string is") print(str1) print("The string after replacing t with d ") print(new_str)

Output

The output of the above program is,

The original string is
Welcome to tutorialspoint
The string after replacing t with d
Welcome do dudorialspoind

Updated on: 19-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements