Python String replace

This Python string function is to replace all occurrences of substring or characters with a new text. The syntax of the Python replace string function is shown below.

Text.replace(old, new, count)
  • Old: Specify the sentence that you want to change.
  • New: This is the new text that the function would substitute for the old original one.
  • Count: Use this optional argument to specify the number of times you want to change the old with the new. If you omit this parameter, alter all occurrences of old with new.

Python String replace Example

This built-in function substitutes all occurrences of the word and returns the copy of the string str object. It is a simple example of this Python string replace function. Here, we are changing l with M.

'Hello World'.replace('l', 'M')
'HeMMo WorMd'

If we change the argument values as (‘o’, ‘ABC’), this method substitutes o with ABC from Hello World, and the output will be ‘HellABC WABCrld’.

In this example, we are using this to change all occurrences of t with X. Remember, this Python string replace function changes case sensitive. It means Python puts only t characters, not T.

a = 'Tutorial Gateway Provides Programming Tutorial'
 
b = a.replace('t', 'X')
 
print(b)
TuXorial GaXeway Provides Programming TuXorial

It is an extension of the above Python string replace function example. Within the first statement, we are putting t with X. In the second statement, a by W.

st = 'tutorial gateway provides programming tutorial'
 
x = st.replace('t', 'X')
print(x)
XuXorial gaXeway provides programming XuXorial

If we use the (‘a’, ‘W’)as the argument values, the output will be tutoriWl gWtewWy provides progrWmming tutoriWl.

Python String replace Multiple Characters Example

In this example method, first, we are changing the tutorial with Alpha. Next, we substituted pro with Di with all occurrences and assigned the result to y.

a = 'tutorial gateway provides this programming tutorial'
 
x = a.replace('tutorial', 'Alpha')
print(x)
Alpha gateway provides this programming Alpha

Substitute Characters at index Example

In this example, we are using Python string replace and the count argument. It means the function substitutes the text a specified number of times. Here, we change the tutorial with tutorialgateway for one time.

text = 'tutorial provides this programming tutorial'
 
y = text.replace('tutorial', 'TutorialGateway', 1)
print(y)
TutorialGateway provides this programming tutorial

In this example, the first statement substitutes the first occurrence of abc with XYZ. The second statement substitutes the first occurrences of abc with substring XYZ.

tt = 'we are abc working at abc company in abc position'
 
x = tt.replace('abc', 'XYZ')
print(x)
 
y = tt.replace('abc', 'XYZ', 1)
print(y)
we are XYZ working at XYZ company in XYZ position
we are XYZ working at abc company in abc position

From the second statement, if we change 1 with 2 (‘abc’, ‘XYZ’, 2), it counts occurrences and changes the first two instances of abc with XYZ. The output is we are XYZ working at XYZ company in abc position.

This program allows the user to enter the text, words to change, and the new text to substitute. Next, we used this Python string replace function to alter the old with the new.

Python Replace String function 5

replace Backslash Example

We are using the Python string replace function to change Backslash with forward-slash.

text = r'C:\Programs\Example Code\xyz.txt'
 
x = text.replace('\\', '/')
print(x)
C:/Programs/Example Code/xyz.txt

In the above example, if you place the ‘Hello’ in the place of ‘/’ then the output will be C:HelloProgramsHelloExample CodeHelloxyz.txt.

It is another example of changing Backslash. Here, we used the third argument (count) to restrict the substitution times.

text = r'C:\Programs\Code\StringExamples\xyz.txt'
 
x = text.replace('\\', '/', 1)
print(x)
C:/Programs\Code\StringExamples\xyz.txt

If you use (‘\\’, ‘/’, 2) as the argument values in the above code, the output will be C:/Programs/Code\String Examples\xyz.txt

replace Backslash with Double slash

This example changes the backslash in a given text with a double-slash using Python string replace.

text = r'C:\Programs\Code\StringExamples\xyz.txt'
print(text)
print()
 
x = text.replace('\\', '\\\\')
print(x)
C:\Programs\Code\StringExamples\xyz.txt

C:\\Programs\\Code\\StringExamples\\xyz.txt

replace string function Regex

This function can search and change one at a time. However, you can use the regex sub-function to search for multiple characters or substrings and change them at a time.

import re
 
text = 'Hello World'
 
x = re.sub('l', 'X', text)
print(x)
HeXXo WorXd

The below program searched for t and an inside a text variable, substituting them with k and ABCD.

import re
 
text = 'tutorial gateway'
 
x = re.sub('[ta]', 'K', text)
print(x)
 
y = re.sub('[ta]', 'ABCD', text)
print(y)
KuKoriKl gKKewKy
ABCDuABCDoriABCDl gABCDABCDewABCDy

It is another approach to using the regular expression or regex in string replace.

import re
 
text = 'Nython programming tutorial gateway'

x = re.sub('t|a', '', text)
print(x)
 
y = re.sub('t|a|g', ' ', text)
print(y)
Nyhon progrmming uoril gewy
Ny hon pro r mmin   u ori l    ew y