Remove Special Characters from String Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

It is always a challenge to remove specific characters from a string. Specific logic has to be implemented to remove a particular character from a string in different programming languages. The python programming language provides in-built functions to remove all special characters from a string.

Scope

  • This article covers the basic concept of string modification in python.
  • This article also explores five methods to remove special characters from a string in python.

How to Remove Special Characters From a String in Python?

In python, strings are immutable objects. Immutable, in this case, implies that the value of a string can't be modified after the assignment but can be reassigned to a new value. Python is a high-level language and provides many string manipulation instructions to perform changes to a string. We can use these string manipulation instructions to remove special characters from a string in python.

We can use the following methods to remove special characters from a string in python,

  • The isalnum() method.
  • Using Regular Expressions(Regex) in python.
  • The replace() method.
  • The filter() method.
  • The translate() method.

We will see how to use all these methods to remove special characters from a string in python with examples in the following sections.

Method 1 : Using the str.isalnum() Method

The string.isalnum() method returns True if all the characters in the string are alphabets or numbers and returns False if it finds any special character in the string. We can use this property to remove all special characters from a string in python. The following code illustrates this,

  • The ".join" function in the above example is used to join all the elements from the list, to create a string with the following syntax,

The iterable may be a list dictionary or tuple, and the connector is of type string and is inserted between every element of the iterable to create a new string.

  • The char for char in special_string if char.isalnum() creates a list of normal characters by performing the isalnum() method on each character of the string separately.
  • The list generator used in the above example can be simplified as follows,

The above code is the simplified version of the previous example used to remove special characters from a string in python. The sample_list is a list of normal characters selected by using the isalnum() function.

For both examples, the output will be,

Method 2 : Using replace()

The replace() method in python can be used to remove specified special characters from the string. The syntax of the replace() method is,

The replace() method replaces a character in the string with another character. There is no effect of the replace() method if the character to be replaced is not found in the string. Using this property, we can remove specific special characters from a string. The following example illustrates this,

In the above example, the special characters in the string are replaced with an empty character using a for loop that iterates all the special characters to be removed from the list.

The output for the above example is,

Method 3: Using Regular Expression

Regular expressions form a pattern we can use to search for special characters in a string. We must import the re package to use regular expressions in Python. This property is used in functions to remove special characters from strings in python. The re package provides us with many methods to use with regular expression. To remove special characters from a string in python, we can use the re.sub() method. The method has the following syntax,

  • The regex_pattern is the regex pattern used to match characters in a string.
  • The replace_char is the character we will replace with the characters matching the regex pattern.
  • The string is the string in which we search and replace of characters.
  • The count depicts the maximum number of replacements we can make after finding matching characters using the regex pattern.
  • The flags represent the regex flags used to modify how we should search. Some useful fags are,
    • re.IGNORECASE which is used to find both lower and upper case characters
    • re.ASCII is used to find only ASCII characters.
    • re.MULTILINE can be used to make ^ and $ symbols to match lines too. Normally, the ^ and $ symbols match the starting and ending of words only.

The following example illustrates how we can remove all special characters from a string in python using the re.sub() method,

The [^A-Z] is used to find all characters that are not between A and Z. The re.IGNORECASE flag has been used to apply the regex pattern to both lower and upper cases. The character matched with the pattern is replaced with an empty string and finally, the new string with special characters removed is returned by the re.sub() method. The third argument, count is given the value of 0 to match all the characters as per the regex.

The default argument for the count and flag parameter is 0 . In the case of the count parameter, the default value of 0 indicates that the function should match all cases and the 0 in the flag parameter indicates that no flags are used.

The output is,

A few other regex patterns that can remove special characters from a string in python are,

  • /[^\w\s]/gi
  • \W+ - this pattern also includes numbers in the result.
  • [@#$*&87]

Method 4: Using filter()

The filter method is used to filter an iterable according to the conditions given in the function. This property is used to remove special characters from strings in python. The syntax of the filter() method is,

The filter function returns an iterable with the filtered results. We can use the filter() method with isalnum() function to remove all special characters from a string. The following code illustrates this,

In the above example, the filter function will invoke the isalnum() function for every character of the special_string and will return a list with all the special characters removed. Then the list is converted to a string by using the join() function.

The filter() method can also be used with a lambda function to remove specific special characters from a string. Let us see an example of how to perform this in python,

In the above example, the special characters we must remove from the string are taken in a list called special_characters. The filter() function is used to iterate over each string element. And the lambda function is used to check if the character is a special character. If a special character is present, the lambda function will return false, and the character will be excluded from the results. This method removes all the special characters from a string in python.

The output for the above examples is,

Method 5: Using translate()

The translate() method uses a dictionary or a mapping table to replace all characters present in the key positions of the table with the characters present in the value position of the table. This property is used to remove special characters from strings in python.

The syntax of translate() method is,

  • The first parameter is a mapping table created with the maketrans() function.
  • The second parameter is optional and represents the characters to be deleted from the string that is being modified.

The syntax for creating a table with maketrans() function is,

The second and third parameters are optional. The keyword, str is used to indicate a string in python.

  • If there is one argument, then it must be a dictionary of type dict.
  • If there are two arguments, then both must be of the type,str and should be of the same length. The maketrans() function will map every character in the first string to every character in the second string.
  • If there are three arguments, the characters in the third argument will be mapped to the value of None.

The following example illustrates how to create a mapping table using a maketrans() function with two arguments,

The above example creates a dictionary or table with each character of the first mapped to characters of the second string. The output for the above example will be,

The maketrans() method saves the characters as their respective ASCII values. For example, the ASCII value of h and b is 104 and 98 respectively.

Let us see how to use this mapping table created using the maketrans() function to remove specific special characters from a string in python,

  • The string.punctuation is a string with all the special characters. This is used to create a dictionary of special characters as keys and empty values as values.
  • The maketrans() method is used to create the table from the created dictionary.
  • Finally, the translate() method removes the special characters by replacing the special characters with empty values and returns the new string.

Note: The value of string.puntuation is ```!"#$%&'()*+, -./:;<=>?@[]^_{|}~

The output of the above example will be,

To remove all the special characters from a string in python, the following python code can be used,

In this example, the string.punctuation is given as the third argument to the maketrans() function, therefore all the characters in the string.punctuation will be mapped to Non. The table created is,

The number in the key position represents the ASCII value of the special characters. The translate uses this table () function to remove all special characters from a string in python.

On calling the translate() function with this table, the special characters will be removed by replacing them with empty characters.

Note: The None keyword in python is used to represent null or empty value.

The output of the above example will be,

Conclusion

  • There are five methods to remove special characters from strings in python.
  • The str.isalnum() method checks a string for the alphabet or number, and this property is used to remove special characters.
  • The replace() method is used to replace special characters with empty characters or null values.
  • Regular expressions match patterns of special characters and remove special characters in python.
  • The filter() method is used to invoke a function for every character of iterable and uses the condition provided in the function to remove special characters in python.
  • The translate() method uses the mapping table and replaces the special characters with characters present in the table.