Home >>Python Programs >How to replace a string with another string in Python
In this example, we will see a Python program through which we can replace any particular string from a given paragraph with any other string.
In this program we will use an inbuilt python module, re module which allows us to solve the various problems based on pattern matching and string manipulation.
Program:
# importing the module
import re
# string
paragraph='''A computer programmer, sometimes called only programmer or more recently a coder, is a person who creates computer software. The term computer programmer can refer to a specialist in one area of computers, or to a generalist who writes code for many kinds of software.'''
# replacing string
reg=re.compile('programmer')
s=reg.sub("developer",paragraph)
# printing the replaced string
print(s)