Q:

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

belongs to collection: Python String Programs

0

Example:

Input: 
"These days, Engineers are struggling to get a job in a better company due 
to the lack of experience and also due to the high competition. 
Engineers have the only book knowledge but the company is expecting the 
industrial experience in the Engineers for better productivity."

Replacing:
"Engineers"with "students"

Output: 
"These days, students are struggling to get a job in a better company due 
to the lack of experience and also due to the high competition. 
Students have the only book knowledge but the company is expecting the 
industrial experience in the students for better productivity."

Algorithm to solve the above problem

  1. Import the re module in the program.
  2. Take paragraph as input also a word which uses to replace a special string.
  3. Print the new paragraph which has replaced string.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

# importing the module
import re

# string
paragraph='''These days, Engineers are struggling to get a job in a better 
company due to the lack of experience and also due to the high competition.
Engineers have the only book knowledge but the company is expecting the 
industrial experience in the Engineers for better productivity.'''
# replacing string

reg=re.compile('Engineers')
s=reg.sub("students",paragraph)

# printing the replaced string
print(s)

Output

These days, students are struggling to get a job in a better
company due to the lack of experience and also due to the high competition.
students have the only book knowledge but the company is expecting the
industrial experience in the students for better productivity.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Find the ASCII value of each character of the stri... >>
<< Extract the mobile number from the given string in...