In addition to returning the fresh string and the number of substitutions, the subn() function is comparable to sub(). Within this guide today, we will be taking a look at the use of the re module along with its subn() function in python to replace single or more characters of a string pattern with new characters.”
Example 01
Let’s get started with the first example of python using the “re” module in it along with its subn() function. So, we have started our code with the import of the “re” module at the first line. After that, we have been initializing a string variable “Str” with a long text of some words in it. The print statement has been utilized here to display the string value of the “Str” variable.
After this, we have been calling the subn() function of the “re” module to replace the character “a” from the string using the second parameter value “a” with the first parametric character value “e” in a string “Str” and save the result within a new variable r1. The subn() function has been utilized again to replace the characters “a” and “I” with the character “e” and save the result within a newly created variable “r2”.
The print statements are used separately to display the updated value of strings r1 and r2 at the console screen of Spyder 3.
Str = "Nature is a healing treatment..."
print(Str)
r1 = re.subn('a', 'e', Str)
r2 = re.subn(‘[a, I]','e',Str)
print(r1)
print(r2)
After saving the above code in the Spyder 3 tool, we executed it quickly and got the shown-below result. The original string has been displayed in the first line of the console without any change, while the next two lines show 2 updated string lines using the “subn()” function of the “re” module in Python. All the characters “a” in a string have been replaced with the character “e” in the 2nd updated outputted string, while the last updated string has been showing the replacement of characters “a” and “I” both with the character “e”.
Along with that, the output for updated r1 and r2 strings shows the total number of characters replaced by the new character. Although we have used the “I” character to be replaced by “e”, it just can’t happen because we don’t have any upper case character “I” in the string.
Example 02
The above example was all about the use of the subn() function to replace one or more characters with a single character restricting the case of a character to be changed. Within this example, we will be replacing a character regardless of its case with another character via the subn() function.
For this, we will be using the “flags” parameter in the function call. So, we have started this code with the same import of the “re” module and called the subn() function of this module at the next line to replace the character “e” with the special character “*” in the given string and displayed the updated string via the print statement. The third line of python code has been demonstrating the use of the same subn() function to replace character “n” with character “*” within the specified string text along with the parameter flags specifying the value “re.IGNORECASE” to ignore the case of a string character “n” for replacement.
The result has been saved to variable “v” and displayed on the shell using the print statement. Within the second and third line, both the string contains at least one capital letter word in it, i.e., N and E. Let’s just save this python script in the spyder 3 file and execute it in the same tool.
print(re.subn('e', ‘*’ , 'NaturE is a healing treatment'))
v = re.subn('n', ‘*' , 'Something is better than Nothing', flags = re.IGNORECASE)
print(v)
The output has been displaying a total of 2 results on our screen. The first result has been displaying the replacement of the small case character “e” from the string without replacing the capital “E” character in the particular string. After that, the second updated string output has been displayed after the replacement of all “n” characters regardless of their cases, i.e., all upper and lowercase “N” characters are not replaced as shown. Along with that, a total number of characters replaced by the subn() function is also displayed.
Example 03
Let’s have our last example of python to use the subn() function of the “re” module in Spyder 3. We have imported the “re” module and created a new string variable named “num,” and initialized it with some mixed characters and numbers. Then, another variable “pattern” has been initialized with “\D” string, i.e., used to remove special characters from a string “(090)-078-601”. The sub() function of the “re” module has been utilized here to remove the special patterns from the “num” string and replace them with nothing, i.e., just delete them from the string.
The result would be saved to the variable “r,” and the print statement would display the result at the end. We have saved this python code and executed it in the Spyder 3 tool in Windows 10 to see the output.
num = '(090)-078-601'
pattern = '\D'
r = re.sub(pattern, ‘ ‘,num)
print(r)
The output has been displaying the num string without its special characters.
Conclusion
This article is all about the use of simple subn(), and sub() functions from python’s “re” module to replace or remove some characters from the given string. We have discussed simple examples to replace the characters with and without the restriction of the upper and lower case, along with the display of the total number of characters replaced by the subn() method.