All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Remove Special Characters From String Python

Last Updated : Mar 11, 2024

Remove Special Characters From String Python

In this tutorial we will show you the solution of remove special characters from string python, removing specific characters from an entire string can be a very difficult task as a programmer.

There may be some requirement to remove not just one malicious character from a string, but more than one. So let’s get started.

Step By Step Guide On Remove Special Characters From String Python :-

Python provides many ways to remove some specific characters or some special characters from a string.

You can use regular expressions to remove special characters, or you can use the str.isalnum() function to remove special characters from a string.

You can also use the filter() function to obtain a string without special characters in it, or you can use Python’s isalpha() function to remove any characters other than the alphabet in a string.

import re
string1 = "Hey! Yo, dude."
new_string1 = re.sub("[^a-zA-Z0-9 ]","",string1)
print(new_string1)
string2 = "Hey! Yo, dude."
new_string2 = ''.join(char for char in string2 if char.isalnum())
print(new_string2)
string3 = "Hey! Yo, dude."
new_string3 = ''.join(filter(str.isalnum, string3))
print(new_string3)
string4="Hey! Yo, dude."
new_string4="".join(filter(str.isalpha,string4))
print (new_string4)
  1. To use regular expressions to remove special characters, you have to use the re.sub() function, and for that, you have to include the first statement at the top of your program.
  2. The next line contains a variable named string1 with the value of “Hey! Yo, dude.”
  3. The next line contains another variable as well, named new_string1, which will hold the value of the resultant string without the special characters in it.
  4. In the regular expression “[^a-zA-Z0-9 ]”, the ^ will represent any alphanumeric characters other than the characters in the brackets, and the “a-zA-Z0-9 ” expression will represent that a string can have only capital or small alphabet characters, numeric digits, and white spaces.
  5. The next line is a print statement which will display the resultant string without any special characters in it.
  6. In the next line, there is another variable named string2 with the value “Hey! Yo, dude.” in it, which contains some special characters.
  7. The next has a variable named new_string2 which will store the string without a single special character in it.
  8. The join() function will iterate through all items and join them into a single string. You must specify a string as a separator.
  9. The isalnum() is a boolean function which will return true if the characters in a string are only alphanumeric and not special characters, otherwise it returns false.
  10. The next line is again a print statement which will display the string after the removal of the special characters as the output.
  11. To remove special characters from a string, you can use the filter() function instead of the for loop and if condition on the isalnum() function. For example, we can use the above function to describe the use of the filter() function.
  12. The filter() function will take two arguments: the first one is the str.isalnum() function, and the second one is the string from which you want to remove special characters.
  13. The next line is again a print statement which will display the output of the string without special characters in it.
  14. The isalpha() function is used to check if the string contains any alphabetic characters or not. If a string contains only the alphabet, then it will return true.
  15. The last statement is again a print statement, which will display the resultant string without any special characters in it.

Conclusion :-

Finally, here in the conclusion, we can say that with the help of this article, you can remove any special characters from a string in a Python program.

You can use any of the methods mentioned above in your program and it will work just fine.

I hope this tutorial on remove special characters from string python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪