Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 replace() Method

Python3 replace() Method

Python3 Strings Python3 Strings


Description

The replace() method replaces old (the old string) with new (the new string) in a string; if the third parameter max is specified, no more than max replacements are made.

Syntax

Syntax of the replace() method:


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

Parameters

Return Value

Returns the new string generated after replacing old (the old string) with new (the new string) in the string; if the third parameter max is specified, no more than max replacements are made.

Example

The following example shows how to use the replace() function:

Example

#!/usr/bin/python3

str

=

"

www.w3cschool.cc

"

print

(

"

Runoob Tutorial old address:

"

,

str

)

print

(

"

Runoob Tutorial new address:

"

,

str

.

replace

(

"

w3cschool.cc

"

,

"

runoob.com

"

)

)

str

=

"

this is string example....wow!!!

"

print

(

str

.

replace

(

"

is

"

,

"

was

"

,

3

)

)

The output of the above example is:


Runoob Tutorial旧地址: www.w3cschool.cc
Runoob Tutorial新地址: www.runoob.com
thwas was string example....wow!!!

Python3 Strings Python3 Strings