Python String replace() Method

5 Sept 2024 | 3 min read

Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Syntax:

Parameters

old: Here this parameter is used for old string which will be replaced.

new: Here this parameter is used for new string which will replace the old string.

count: The count parameter is used to describe the number of times to process the replace.

Return

It returns string

Let's see some examples of replace() method to understand it's functionality.

Example 1

Output:

The Old String before performing the replace method is: Java is a programming language The new String after performing the replace method is: C is a programming language 

Example 2

Output:

Old String: The Old String before performing the replace method is: Java C C# Java Php Python Java The new String after performing the replace method is: C# C C# C# Php Python C# The Old String before performing the replace method is: Java C C# Java Php Python Java The new String after performing the replace method is: C# C C# Java Php Python Java 

Example 3

Output:

Tomato is also a fruit 

Next TopicPython Strings