Python String replace

The Python string replace function replace string function is to replace all occurrence of an existing string or character(s) with a new substring. The syntax of the Python replace function is

String_Value.replace(old_string, new_string, count)
  • String_Value: A valid String literal.
  • Old_string: Specify the substring that you want to replace.
  • New_string: This is the new substring that would replace the Old_string.
  • Count: Use this optional argument to specify the number of times you want to replace the old_string with new_string. If you Omit this parameter, the Python string replace Function replace all occurrence of old_string with new_string.

Python replace String Example

It is a simple example of this string replace function. Here, we are replacing l with M, o with A, and o with ABC from Hello World.

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

'Hello World'.replace('o', 'A')

'Hello World'.replace('o', 'ABC')

String replace function output

>>> 'Hello World'.replace('l', 'M')
'HeMMo WorMd'
>>> 'Hello World'.replace('o', 'A')
'HellA WArld'
>>> 'Hello World'.replace('o', 'ABC')
'HellABC WABCrld'
>>> 

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

# String Replace Example
 
string = 'Tutorial Gateway Provides Python Programming Tutorial'
 
x = string.replace('t', 'X')
 
print(x)
TuXorial GaXeway Provides PyXhon Programming TuXorial

It is an extension of the above Python string replace function example. Within the first statement, we are replacing t with X. In the second statement, a replaced by W. Next, the third statement replace e with Z.

string = 'tutorial gateway provides python programming tutorial'
print(string)
 
x = string.replace('t', 'X')
print(x)
 
y = string.replace('a', 'W')
print(y)
 
z = string.replace('e', 'Z')
print(z)
tutorial gateway provides python programming tutorial
XuXorial gaXeway provides pyXhon programming XuXorial
tutoriWl gWtewWy provides python progrWmming tutoriWl
tutorial gatZway providZs python programming tutorial

Python String replace Multiple Characters Example

In this String method example, first, we are replacing the tutorial with Alpha. Next, we are replacing pro with Di and assigned the result to y.

string = 'tutorial gateway provides python programming tutorial'
print(string)
 
x = string.replace('tutorial', 'Alpha')
print(x)
 
y = string.replace('pro', 'Di')
print(y)
tutorial gateway provides python programming tutorial
Alpha gateway provides python programming Alpha
tutorial gateway Divides python Digramming tutorial

Python replace Character in String at index Example

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

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

TutorialGateway provides python programming TutorialGateway
TutorialGateway provides python programming tutorial

In this example, the second statement replaces the first occurrence of abc with XYZ. The third statement replaces the first two occurrences of abc with substring XYZ. And the fourth statement replaces the first three instances of abc with XYZ.

string = 'we are abc working at abc company in abc position'
print(string)
 
x = string.replace('abc', 'XYZ')
print(x)
 
y = string.replace('abc', 'XYZ', 1)
print(y)
 
z = string.replace('abc', 'XYZ', 2)
print(z)
 
m = string.replace('abc', 'XYZ', 3)
print(m)
we are abc working at abc company in abc position
we are XYZ working at XYZ company in XYZ position
we are XYZ working at abc company in abc position
we are XYZ working at XYZ company in abc position
we are XYZ working at XYZ company in XYZ position

Practical String replace Example

This program allows the user to enter the string, string to be replaced, and the new string to replace with. Next, we used the replace function to replace the old string with new.

string = input("Please enter any String :  ")
old_string = input("Please enter the String to replace : ")
new_string = input("Please enter the new String : ")
 
x = string.replace(old_string, new_string)
print(x)
 
y = string.replace(old_string, new_string, 1)
print(y)
 
z = string.replace(old_string, new_string, 2)
print(z)
Python Replace String 5

Python replace Backslash

We are using Python string replace function to replace Backslash with forward-slash, :, and Hello.

# Python string replace Backslash Example
 
text = r'C:\Programs\Python Programs\xyz.txt'
 
x = text.replace('\\', '/')
print(x)
print()
 
y = text.replace('\\', ':')
print(y)
print()
 
z = text.replace('\\', 'Hello')
print(z)

String replace Backslash with forward slash output

C:/Programs/Python Programs/xyz.txt

C::Programs:Python Programs:xyz.txt

C:HelloProgramsHelloPython ProgramsHelloxyz.txt

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

text = r'C:\Programs\Python\Python Programs\xyz.txt'
 
x = text.replace('\\', '/', 1)
print(x)
print()
 
y = text.replace('\\', '/', 2)
print(y)
print()
 
z = text.replace('\\', '?', 3)
print(z)
print()
 
a = text.replace('\\', ' Hi ', 3)
print(a)
C:/Programs\Python\Python Programs\xyz.txt

C:/Programs/Python\Python Programs\xyz.txt

C:?Programs?Python?Python Programs\xyz.txt

C: Hi Programs Hi Python Hi Python Programs\xyz.txt

replace Backslash with Double slash

This string replace example replaces the backslash in a given text with double-slash

#  Python string replace Backslash with Double Backslash Example
 
text = r'C:\Programs\Python\Python Programs\xyz.txt'
print(text)
print()
 
x = text.replace('\\', '\\\\')
print(x)
C:\Programs\Python\Python Programs\xyz.txt

C:\\Programs\\Python\\Python Programs\\xyz.txt

Python replace Regex

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

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

HeXXo WorXd

The below string replace function program search for t and a, inside a text variable, and replace them with k and ABCD.

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

KuKoriKl gKKewKy
ABCDuABCDoriABCDl gABCDABCDewABCDy

It is another approach of Python string replace using the regular expression or regex.

import re
 
text = 'python programming tutorial gateway'
print(text)
print()
 
x = re.sub('t|a', '', text)
print(x)
 
y = re.sub('t|a|g', ' ', text)
print(y)
python programming tutorial gateway

pyhon progrmming uoril gewy
py hon pro r mmin   u ori l    ew y