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

Python replace() method

Python replace() method

Python Strings Python Strings


Description

The Python replace() method replaces old (old string) in a string with new (new 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/python

str

=

"

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

"

;

print

str

.

replace

(

"

is

"

,

"

was

"

)

;

print

str

.

replace

(

"

is

"

,

"

was

"

,

3

)

;

The output of the above example is:


thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string

Python Strings Python Strings