The sub() function

This is one of the most important functions of the re module. The sub() is used for replacing the re pattern with the specified replacement. It will replace all the occurrences of the re pattern with the replacement string. The syntax is as follows:

            re.sub(pattern, repl_str, string, count=0)
  • pattern: The re pattern.
  • repl_str: The replacement string.
  • string: The main string.
  • count: The number of occurrences to be replaced. The default value is 0, which means replacing all occurrences.

Now we are going to create a re_sub.py script and write the following content in it:

import re

str_line = 'Peter Piper picked a peck of pickled peppers. How many pickled peppers did Peter Piper pick?'

print("Original: ", str_line)
p = re.sub('Peter', 'Mary', str_line)
print("Replaced: ", p)

p = re.sub('Peter', 'Mary', str_line, count=1)
print("Replacing only one occurrence of Peter… ")
print("Replaced: ", p)

Run the script and you will get the output as follows:

student@ubuntu:~/work$ python3 re_sub.py
Original: Peter Piper picked a peck of pickled peppers. How many pickled peppers did Peter Piper pick?
Replaced: Mary Piper picked a peck of pickled peppers. How many pickled peppers did Mary Piper pick?
Replacing only one occurrence of Peter...
Replaced: Mary Piper picked a peck of pickled peppers. How many pickled peppers did Peter Piper pick?

In the preceding example, we used sub() to replace the re pattern with a specified replacement string. We replaced Peter with Mary. So, all the occurrences of Peter will be replaced by Mary. Next, we also included the count parameter. We mentioned count=1: it means only one occurrence of Peter will be replaced and other occurrences of Peter will remain the same.

Now, we will learn about the subn() function of the re module. The subn() function works the same as sub() with the additional functionality. The subn() function will return a tuple containing the new string and the number of replacements performed. Let's look at an the example of subn(). Create a re_subn.py script and write the following content in it:

import re

print("str1:- ")
str1 = "Sky is blue. Sky is beautiful."

print("Original: ", str1)
p = re.subn('beautiful', 'stunning', str1)
print("Replaced: ", p)
print()

print("str_line:- ")
str_line = 'Peter Piper picked a peck of pickled peppers. How many pickled peppers did Peter Piper pick?'

print("Original: ", str_line)
p = re.subn('Peter', 'Mary', str_line)
print("Replaced: ", p)

Run the script and you will get the output as follows:

student@ubuntu:~/work$ python3 re_subn.py
str1:-
Original: Sky is blue. Sky is beautiful.
Replaced: ('Sky is blue. Sky is stunning.', 1)

str_line:-
Original: Peter Piper picked a peck of pickled peppers. How many pickled peppers did Peter Piper pick?
Replaced: ('Mary Piper picked a peck of pickled peppers. How many pickled peppers did Mary Piper pick?', 2)

In the preceding example, we used the subn() function to replace the RE pattern. As a result, we got a tuple containing the replaced string and the number of replacements.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset