
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- How to replace all occurrences of a word in a string with another word in java?
- Replace All Occurrences of a Python Substring with a New String?
- How to replace all occurrences of a string in JavaScript?
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- Java Program to replace all occurrences of given String with new one
- Java Program to replace all occurrences of a given character in a string
- Replace part of a string with another string in C++
- Replace all words with another string with Java Regular Expressions
- Replace String with another in java.
- C++ program to replace all occurrences of string AB with C without using extra space
- Replace one string with another string with Java Regular Expressions
- How to use JavaScript to replace a portion of string with another value?
- How to replace total string if partial string matches with another string in R data frame column?
- Java Program to replace only first occurrences of given String with new one
