Python program to remove special characters from a string

How to remove all special characters from a string in python:

In Python, we can remove all special characters from a string in different ways. Not only special characters, we can use these same methods with a slight modification to remove any other characters from a string as well.

In this post, I will show you these processes. The most basic one is to use a loop, iterate through each character and remove all special characters. Since string is immutable, or we can’t modify it, we need to create one different string in all of these cases.

Let’s check them one by one.

Method 1: By iterating through the characters:

In this method, we will iterate through the characters of the string one by one. For each character, we will check if it is alphanumeric or not by using character.isalnum() method. At the beginning, we will create one empty string. For the characters, which are alphanumeric, we will add them to this empty string. Finally, this string will hold the characters which are not special character or it will hold the string without any special character.

Below is the program:

def remove_special_char(s):
    result_string = ''
    for ch in s:
        if ch.isalnum():
            result_string += ch

    return result_string


if __name__ == '__main__':
    input_string = input('Enter a string: ')
    final_string = remove_special_char(input_string)

    print('After special characters removed: {}'.format(final_string))

Here,

  • removespecialchar method removes the special characters from the given string s.
  • result_string is the final string. It is initialized as an empty string.
  • It iterates through the characters of the string s one by one using a for loop.
  • For each character, it checks if it is alphanumeric or not by using isalnum method. If it is alphanumeric, it adds it to result_string.
  • Finally, result_string is returned.
  • main is called first. It takes one string as input from the user, passes it to removespecialchar to remove all special characters and prints the newly created string.

If you run this program, it will print output as like below:

Enter a string: hello@#$%^*one#$two#@three
After special characters removed: helloonetwothree

Note that, spaces are also considered as special characters in this script.

Using list comprehension:

We can optimize the above solution using list comprehension as like below:

def remove_special_char(s):
    return ''.join(ch for ch in s if ch.isalnum())


if __name__ == '__main__':
    input_string = input('Enter a string: ')
    final_string = remove_special_char(input_string)

    print('After special characters removed: {}'.format(final_string))

Here,

  • the code inside join is creating a list of only alphanumeric characters.
  • The join method joins all characters in the list with no space between them.
  • So, basically, it is doing the same process. It is taking the string, picking the characters and joining them to build the final string.

It will give similar output.

Using filter:

This is almost similar. But here, we will use filter to filter out the alphanumeric characters and using join, we can join them similar to the above.

def remove_special_char(s):
    filtered_char = filter(str.isalnum, s)
    return ''.join(filtered_char)


if __name__ == '__main__':
    input_string = input('Enter a string: ')
    final_string = remove_special_char(input_string)

    print('After special characters removed: {}'.format(final_string))

The filter method finds the alphanumeric characters and the join methods joins them to produce the final string.

Using Regex:

Regular expression or regex is another way to solve this problem. We can remove all from a string those are not matching characters or numbers.

import re

def remove_special_char(s):
    return re.sub('[^A-Za-z0-9]+', '', s)

if __name__ == '__main__':
    input_string = input('Enter a string: ')
    final_string = remove_special_char(input_string)

    print('After special characters removed: {}'.format(final_string))

Here,

  • We are using the re module, which is used for regular expression or regex in python.
  • The regex string matches for all characters which are not lower-case, upper-case and numbers in the given string and replaces them with blank. i.e. we are removing the special characters from the string.
  • You can modify the regex to include any specific characters that you don’t want to remove. For that, just add that character after 0-9 and before ]+.

It will give similar result.

python remove special characters from string

You might also like: