replace () in python to replace a substring

Python Methods and Functions | String Variables

Examples:

 Input: str = "helloABworld" Output: str = "helloCworld" Input: str = "fghABsdfABysu" Output: str = "fghCsdfCysu" 

This problem has an existing solution, please refer. 

# Function to replace all occurrences of AB with C

 

def replaceABwithC ( input , pattern, replaceWith):

return input . replace (pattern, replaceWith)

 
# Driver program

if __ name__ = = "__ main__" :

input   = 'helloABworld'

  pattern = ' AB'

replaceWith = 'C'

  print replaceABwithC ( input , pattern, replaceWith)

Exit:

 'helloCworld' 




Tutorials