Python String replace

This Python string function is to replace all occurrences of substring or characters with a new text and the syntax is

Text.replace(old, new, count)
  • Old: Specify the sentence that you want to change.
  • New: This is the new text that the Python replace string 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 new. If you omit this parameter, alter all occurrences of old with new.

Python String replace Example

This built in function substitutes all occurrence 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 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 are substituting 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 along with the count argument. It means the function substitute the text for the 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 substitute 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 count 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, word to change, and the new text to substitute. Next, we used this python string replace function to alter the old with new.

Python Replace String 5

Backslash Example

We are using 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\String Examples\xyz.txt'
 
x = text.replace('\\', '/', 1)
print(x)
C:/Programs\Code\String Examples\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

Change Backslash with Double slash

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

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

C:\\Programs\\Code\\String Examples\\xyz.txt

replace 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 search for t and a, inside a text variable, and substituted 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 of using the regular expression or regex in python string replace.

import re
 
text = 'python programming tutorial gateway'

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