पायथन मॉड्यूल re में re.sub() मेथड है जो पैटर्न खोजने और उसे नई सबस्ट्रिंग से बदलने में मदद करता है। यदि पैटर्न नहीं मिलता है, तो स्ट्रिंग अपरिवर्तित लौटाई जाती है।
re.sub() का सिंटैक्स:
re.sub(pattern, repl, string):
उदाहरण के लिए नीचे दिए गए कोड में, हम 'India' खोजते हैं और इसे 'the World' से बदलते हैं स्ट्रिंग 'TP is most popular Tutorials site of India' में।
उदाहरण
result = re.sub(r'India', 'the World', 'TP is the most popular Tutorials site of India')\nprint(result)
आउटपुट
TP is the most popular Tutorials site of the World