06.0 Python‎ > ‎Python Notes‎ > ‎String Processing‎ > ‎

.replace()

.replace() will return a string object that replaces one sub-string for another sub-string inside a the original string object.
The original string is not changed by the .replace() method

Syntax

   new_string = original_string.replace(thisStr,thatStr)

OR
   original_string.replace(thisStr,thatStr)

target_string
thisStr
thatStr

Example

str ="mississip,pim"
#remove the last m
newStr = str.rstrip('m')
#replace the , with nothing -- ""
newStr = newStr.replace(",","")
#replace all i in the string with *
newStr = newStr.replace("i","*")
print (newStr)
Comments