How to Replace Matches with Text of Your Choice in Python


In this example, we will learn how to replace the matches with the text of your choice using the sub() function in Python.

Source Code

import re

str1 = 'I love Python'

r = re.sub('o', 'O', str1)

print(r)

Output:

I lOve PythOn
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement