- sub() is a python regular expression function that are used to replace the matches with the new string.
Syntax-
re.sub(pattern,newmatch,string)
Example-
string is s="This is programming language"
re.sub("s","-",s) will return "This-is-programming-language"
program-
import re
print("Enter the string")
s=input()
print("Enter part of string that want to replace")
old=input()
print("Enter something that replace the given match")
new=input()
x=re.sub(old,new,s)
print("nInput string is",s)
print("String After replace is",x)
Enter the string
This-Is-The-String
Enter part of string that want to replace
-
Enter something that replace the given match
_
Input string is: This-Is-The-String
String After replace is: This_Is_The_String