Python string replace method

Replace a substring

The replace method returns a string where all occurrence of one string is replaced by another string.

The replace method has the following syntax.

s.replace(old, new [, count])

Returns a copy of string s with all occurrences of substring old replaced by new. If count is passed, the first count occurrences are replaced. This works like a combination of x=s.split(old) and new.join(x).


print 'This is a test'.replace('is', 'eez') 
# ww w. j  a  v  a  2 s. c  o m
string1 = "One, one, one, one, one, one"

print string1.replace( "one", "two" )
print "Replaced 3 maximum:", string1.replace( "one", "two", 3 )




















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary