How to Replace Matches in Python


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

Source Code

import re

str1 = 'I love Python'

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

print(r)

Output:

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