×

Python String replace() method

Python String replace() method

The string.replace() method in Python returns a copy of the string with all occurrences of substring old replaced by new.

Syntax

replace(old, new[, count])

Parameter

old: This parameter represents a substring you want to replace.

new: This parameter represents a substring which would replace the old substring.

count(optional): It signifies the number of times you want to replace the old substring with the new substring.

Return

This method returns a copy of the string where all occurrences of a substring is replaced with another substring. 

Example 1

 # Python program explaining
 # the string.replace() method
 # initializing the String
 string ="Virat Kohli is my favourite cricketer."
 print("Actual string:",string)
 # replacing virat kohli with Ms Dhoni
 val = string.replace("Virat Kohli", "Ms Dhoni")
 print("After replacing the string: ")
 # printing the replaced string
 print(val) 

Output

 Actual string: Virat Kohli is my favourite cricketer.
 After replacing the string: 
 Ms Dhoni is my favourite cricketer. 

Example 2

 # Python program explaining
 # the string.replace() method
 # initializing the String
 string ="She sells seashells by the seashore"
 print("Actual string:",string)
 # replacing virat kohli with Ms Dhoni
 val = string.replace("e", "i")
 print("After replacing the string... ")
 # printing the replaced string
 print(val)
 # replacing only the first two 'e' with 'i'
 val = string.replace("e", "i",2)
 print("After replacing only the first 2 'e' characters... ")
 # printing the replaced string
 print(val) 

Output

 Actual string: She sells seashells by the seashore
 After replacing the string... 
 Shi sills siashills by thi siashori
 After replacing only the first 2 'e' characters... 
 Shi sills seashells by the seashore 

Related Topics

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Removing the First Character from the String in Python

String The string is the collection of characters. The strings in python are “immutable”; we cannot change a string once they are created. In python, whatever data we take as input...

4 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Hog Descriptor Opencv Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

Read Text files in Python

In Python, there are many ways to read text files. Before going into the detailed structure of reading a text file, let us understand how reading text files takes place...

4 minutes read.

How to Iterate a List in Python

As we know, a List is one of the four unique data structures available in Python; in this article, we will deep dive into understanding iterating through a list and...

3 minutes read.

Return Statement In Python

Python Return Statement The return statement is generally used for the execution ending and returns the value back to the caller. The return statement can return all types of values and...

2 minutes read.

Io stringio Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

7 minutes read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

Python program to count and display vowels in a string

Python program to count and display vowels in a string This python program counts the vowels in a string and displays them on the screen. We can do this in several...

2 minutes read.

An Introduction to Mocking in Python

Python Mocking is a testing library. It enables you to substitute portions of your system under test with fake(mock) objects and make assertions about how they were utilized.  The test is...

3 minutes read.

Python slice()

Python slice() class The slice() class return a slice object representing the set of indices specified by range(start, stop, step).  Syntax class slice(stop)          or class slice(start, stop[, step]) Parameter Start: This parameter represents an integer number specifying at...

1 minute read.

Import Function in Python

In Python programming language, the import keyword plays an important role in the whole code because it makes one module make available in another module. Import is used to structure...

3 minutes read.

Division in Python

In this tutorial, we will understand what mathematical operation division is and how is it performed in Python Programming language. We will also focus on the operators being used in...

3 minutes read.

Python Dictionary

Dictionary in Python is an unordered collection of data values, which is used to store data values like a map. Unlike different data types that hold just individual assets as...

4 minutes read.

*Args and **Kwargs in Python

Let’s know about ** (dual star / asterisk) and * (star / asterisk) are operators in Python. These stars are used in Python for parameters. Args and kwargs are special...

3 minutes read.

Python List pop() method

Python List pop() method The list.pop() method removes the item at the specified position in the list, and return it. If no index is specified, this method removes and returns the last item in the list. Syntax list.pop([i]) Parameter i:...

1 minute read.

Python Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 minutes read.