string.replace in Python

Python Methods and Functions

Returns a copy of the string that replaces all occurrences of the specified string with the specified value.

string.replace (s, old, new [, maxcount])

-> str

s - The string in which to replace the desired one.

old - The desired substring, which should be replaced.

new - The substring to replace the searched for. 

  my_str = 'barbarian' 
my_str = my_str.replace (' bar', 'mur') # 'murmurian'
my_str = my_str.replace (' mur', 'bur', 1) #' burmurian'


Function not available in Python 3. Use appropriate string method .

Get Solution for free from DataCamp guru