Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python String replace

by suresh

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')
Python string replace

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)
Python String Replace 1

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)
Python String Replace 2

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)
Python String Replace 3

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)
Python string replace 6

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)
Python Replace String 4

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)
Python replace 7

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)
Python replace 8

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)
Python replace 9

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)
Python replace 10

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)
Python replace 11

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 replace 12

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy