×

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

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

Python program to sort the array element into ascending order

Python program to sort the array element into ascending order In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a...

2 minutes read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.

Python program to perform the arithmetic operation

Python program to perform the arithmetic operation This program will write a code to perform some basic arithmetic operations like addition, subtraction, multiplication, exponent, modulus, and division. Here we first need...

2 minutes read.

Python String rfind() method

The string. rfind() method in Python returns the highest index in the string if the substring sub is found else it returns -1 if the substring is not found. Syntax string.rfind(sub [,start [,end]]) Parameter sub: This parameter...

2 minutes read.

Python Read Excel file

Python Read Excel file Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the...

3 minutes read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

Fsolve in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

What is a Script in Python

Have you ever heard that Python is a scripting language? Or when people address a Python program file as a script? Besides programming, script generally means "a written story for...

3 minutes read.

How to clear screen in Python?

How to clear screen in python There are times when we execute our program and it results in an unexpected output. The obtained result can contain some kind of garbage values...

4 minutes read.

The Calendar Module of Python

The calendar module provides calendar features, including printing functions for a specific month or year. Default is the first day of the week on Monday and the last day on Sunday...

3 minutes read.

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

3 minutes read.

Count Number of Keys in Dictionary Python

Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count,...

3 minutes read.

How to Install Python

Python Installation Guide Step by Step Installing Python is quite simple and easy. In this tutorial, we will show stepwise procedure to install Python and set up the Python environment in different...

2 minutes read.

How to read data from com port in python

Comport, the I/O interface is known as a COM port that allows the connection for a serial device to a computer. COM ports are sometimes referred to as serial ports....

3 minutes read.

Permutations in Python

Recursion Basic idea: for numbers of length N. N-1 items are chosen at random between 0 and then generate permutations using the remaining N-1 elements in a recursive fashion. Once you've done...

4 minutes read.

Python Linear regression

In this tutorial, we will understand the meaning, usage, and types of Linear Regression. Further, we will comprehend the terms cost function and Optimization. Linear Regression is the widely used Machine...

3 minutes read.