Python

Remove Special Characters from String Python

There are many cases where you got errors while coding due to the use of some special characters within your code. This might be caused due to use of these special characters within the strings and with code statements as well (in the case if wrong special characters were used). So, we have thought of implementing the article on removing these special type characters from string type variable values while working in a Python environment. So, let’s begin with opening the shell.

Example 1: Join() with Isalnum()

To create a code, you need a python file. Try to create one with the “touch” command in the shell. The file name must contain the python extension with it. After the creation of a Python type file, open it within some editor. You can make use of any built-in editor i.e Vim, Nano, and text editor. We have a GNU Nano editor here. So, let’s quickly open it as shown below.

$ touch remove.py
$ nano remove.py

The file has been opened successfully in the editor. Added the python-support path within it to make it executable. We have initialized a long string type variable v1 with some special and normal characters in it. The print statement has been used here to display this string-type variable value on the shell as it is.

After this, we have used the “isalnum()” method to get only simple characters from this string and ignore the special characters. All the normal characters will be joined together to be saved in the variable v2 with the help of the method “join()”. The newly updated string would be displayed on the shell with the help of a print statement.

#!/usr/bin/python3
v1 = “HELLO! This, is@an’Example String:”;
print (“Original string: ”, v1);
v2 = ‘’.join(char for char in v1 if char.isalnum())
print (“Updated string: ”, v2);

After running this file with the python3 keyword, we have got the original string with special characters and the updated string with no special characters in it as beneath.

$ python3 remove.py

Example 2: Join() with Filter()

Let’s make use of the join method simply. Open the same file and initialize a string with normal characters and special characters. The original string will be displayed first. We have been using the join method with filter() function on the string variable v1 to filter out the characters. The “isalnum()” method has been used to collect only normal characters from the variable v1. The updated string has been printed out.

#!/usr/bin/python3
v1 = “HELLO! This , is@an’Example String:”;
print (“Original string: ”, v1);
v2 = ‘’.join(filter(str.isalnum, v1))
print (“Updated string: ”, v2);

The result is the same as we got in the above example while using this simple method.

$ python3 remove.py

Let’s update the code once again. We have used a long string with all the special characters on our keyboard. The same join(), filter() and isalnum() function are used once again. Print statements have been used to display the original string and updated string with no special characters in it on the shell.

#!/usr/bin/python3
v1 = “a!b#c$d%e^f&g*h (i)j_k~l`m/n?o.p>q’ r]s[t{u}v=w+x-y\z”;
print (“Original string: ”, v1);
v2 = ‘’.join(filter(str.isalnum, v1))
print (“Updated string: ”, v2);

In return, we have got the original long string with special characters first and then the updated string having no special characters i.e. only characters within the string.

$ python3 remove.py

Example 3: “Re” Package with “Sub” Method

Let’s make use of the “sub” function of a remove i.e. “re” package of python to remove special characters. So, import the “re” package in the code first. Initialize a string and display using the print statement. The “sub()” method is used with the “r” flag to remove all the special characters from a string except the specified ones and the updated string would be displayed.

#!/usr/bin/python3
Import re
v1 = “HELLO! This , is@an’Example%String:”;
print (“Original string: ”, v1);
v2 = re.sub(r”[^a-Za-Z0-9], “”, v1)
print (“Updated string: ”, v2);

As a result, we have got the original string and the updated one.

$ python3 remove.py

Conclusion

This guide contains the implementation of using the different methods to remove special characters from some string values. We have used the “re” package, join(), filter(), and isalnum() method to achieve it.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content