Replace a special string from a given paragraph with another string in Python

Replace a special string from a given paragraph with another string in Python

Supplant an exceptional string from a given passage with another string in Python

Here, we will perceive how to supplant an exceptional string from a given passage with another given string or word in the Python programming language?

Here and there, we need to supplant a string or expression of a given section by another string or word and we can do it by altering the passage via looking through the word however when the length of the passage is too huge then we feel tired in the wake of doing this. Along these lines, to defeat this issue we will figure out how to do very similar things in Python and furthermore we will utilize the re module of Python to make it so natural.

Python has an inbuilt re module which enables us to take care of the different issues dependent on design coordinating and string control. To comprehend the issue essentially, how about we take an example.

Example:

Input: 
"Need more than one section? You can indicate what number of passages you need by changing the primary number in the Rand articulation. For example, on the off chance that you required two sections (with five sentences in every one), you could type this"
Replacing:
"need" with "requirement"
"requirement more than one section? You can indicate what number of passages you requirement by changing the primary number in the Rand articulation. For example, on the off chance that you required two sections (with five sentences in every one), you could type this"

Calculation to take care of the above issue,

  1. Import the re module in the program.
  2. Accept section as info additionally a word which uses to supplant an extraordinary string.
  3. Print the new passage which has supplanted string.

Program:

# importing the module
import re

///// property of JustTechReview

# string
paragraph='''Need more than one section? You can indicate what number of passages you need by changing the primary number in the Rand articulation. For example, on the off chance that you required two sections (with five sentences in every one), you could type this'''
# replacing string

reg=re.compile('need')
s=reg.sub("requirement",paragraph)

# printing the replaced string
print(s)

Output:

"requirement more than one section? You can indicate what number of passages you requirement by changing the primary number in the Rand articulation. For example, on the off chance that you required two sections (with five sentences in every one), you could type this"

Recent Posts

error: Alert: Content is protected!!