Easy Tutorial
❮ Python Att List Clear Python Multiply List ❯

Python3 replace() Method

Python3 Strings


Description

The replace() method replaces the old substring in the string with the new substring. If the third parameter max is specified, it replaces no more than max occurrences.

Syntax

The syntax for the replace() method is:

str.replace(old, new[, max])

Parameters

Return Value

Returns a new string where the old substring is replaced with the new substring. If the third parameter max is specified, it replaces no more than max occurrences.

Example

The following example demonstrates the use of the replace() function:

#!/usr/bin/python3

str = "www.w3cschool.cc"
print ("tutorialpro.org old address:", str)
print ("tutorialpro.org new address:", str.replace("w3cschool.cc", "tutorialpro.org"))

str = "this is string example....wow!!!"
print (str.replace("is", "was", 3))

The output of the above example is:

tutorialpro.org old address: www.w3cschool.cc
tutorialpro.org new address: www.tutorialpro.org
thwas was string example....wow!!!

Python3 Strings

❮ Python Att List Clear Python Multiply List ❯