Home » Python String | replace() method with Examples

Python String | replace() method with Examples

by Online Tutorials Library

Python String replace() Method

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.

Signature

Parameters

old : An old string which will be replaced.

new : New string which will replace the old string.

count : 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.

Python String replace() Method Example 1

Output:

Old String:    Java is a programming language  New String:    C is a programming language  

Python String replace() Method Example 2

Output:

Old String:    Java C C# Java Php Python Java  New String:    C# C C# C# Php Python C#     Old String:    Java C C# Java Php Python Java  New String:    C# C C# Java Php Python Java  

Python String replace() Method Example 3

Output:

Tomato is also a fruit  

Next TopicPython Strings

You may also like