Explain subn() methods of “re” module in Python ?

A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The re module in python refers to the module Regular Expressions (RE). It specifies a set of strings or patterns that matches it. Metacharacters are used to understand the analogy of RE. 

subn()method is similar to sub() and also returns the new string along with the no. of replacements.


re.subn (pattern, repl, string, count=0, flags=0)


import re
print(re.subn('ov', '~*' , 'movie tickets booking in online'))
t = re.subn('ov', '~*' , 'movie tickets booking in online', flags = re.IGNORECASE)

Here you can see that, subn() method It returns a tuple with count of total of all the replacements as well as the new string.


('m~*ie tickets booking in online', 1)
('m~*ie tickets booking in online', 1)
m~*ie tickets booking in online