| by Arround The Web | No comments

Remove Special Characters From String Python

Strings” are one of the most commonly used data types in Python and are used to store text and can contain a combination of letters, numbers, and special characters. If you are working with Python strings, you may come across the need to remove special characters from a string. Special characters are characters that hold a particular significance/meaning and can be anything from punctuation marks to symbols, white spaces, etc.

In this article, we will explore the approaches to eliminating special characters from a given Python string.

How to Remove/Eliminate Special Characters From Particular Python String?

To remove/eliminate special characters from a particular Python string, consider the below-listed approaches:

Method 1: Remove Special Characters From String in Python Utilizing the “string.replace()” Method

The “string.replace()” method is utilized to replace all instances of a specified substring in a string with a new substring. A Python string can be modified by removing special characters with this method.

Syntax

string.replace(old, new, count)

The “string.replace()” method has three parameters: “old” for the substring to replace, “new” for the replacement string, and “count” (optional) for the maximum number of occurrences to replace.

Example
Here is a brief overview of the code:

string_value = "@Hello@and#Welcome%to&Python"
for char in "!@#$%^&*()_+-={}[]|\:;"'<>,.?/~`":
    string_value = string_value.replace(char, " ")
print(string_value)

According to the above code:

  • The string variable containing special characters is initialized.
  • After that, the “for” loop is used to iterate through the initialized string comprising the special characters we want to remove.
  • Lastly, the “replace()” method is applied to replace each special character with an empty string.

Output

In the code snippet above, the special characters were removed and substituted with an empty string.

Method 2: Remove Special Characters From String in Python Using the “join()” and “filter()” Methods

The “join()” function concatenates a list of strings to create a single string, using a designated separator between each string. The “filter()” function is utilized to extract elements from an iterable list, based on a specified function. Both of these methods can also be utilized to eliminate special characters from a string in Python.

Syntax

separator.join(iterable)

In the above syntax, the parameter named “separator” is utilized for the separator string, and the parameter named “iterable” is used for the iterable object containing the elements to join.

filter(function, iterable)

The “filter(function, iterable)” method takes two parameters: “function” for the function that tests each element of an iterable and “iterable” for the sequence or iterable to be filtered.

Example
Let’s go through the following code:

string_value = "@Hello@and#Welcome%to&Python"
new_string_value = "".join(filter(str.isalnum, string_value))
print(new_string_value)

In the above code snippet:

  • Likewise, the string variable containing special characters is initialized.
  • The “filter()” method is utilized to filter any characters that are not alphanumeric from the given string.
  • The “join()” method joins the filtered characters back into a string.

Output

As analyzed, the special characters are filtered out with empty strings.

Method 3: Remove Special Characters From String in Python Using “Regular Expression”

A “Regular Expression” is a character string that describes a specific search pattern They can be used to search, replace, or manipulate strings. This approach can be used to eliminate special characters from the input string.

Example
Consider the following example code:

import re
string_value = "@Hello@and#Welcome%to&Python"
pattern = r"[^\w\s]"
new_string_value = re.sub(pattern, " ", string_value)
print(new_string_value)

According to the above code, perform the following steps:

  • Regular expressions can be used by importing the “re” module.
  • In the next step, the string variable containing special characters is initialized.
  • Also, define a pattern that matches any character that is not a word or whitespace using the “[^]” notation.
  • Finally, the “re.sub()” method replaces all matches of the pattern with a null/empty string.

Output

In the above snippet, it can be seen that the special characters have been replaced with empty strings.

Method 4: Remove Special Characters From String in Python Via Looping

Another way to eliminate special characters from a particular string in Python is via the “for” loop by placing an exception upon the string.

Example
The below-stated example applies the looping to omit the special characters from the string:

string = "@Hello@and#Welcome%to&Python"
new_string = ""
for char in string:
    if char.isalnum() or char.isspace():
        new_string += char
print(new_string)

In the above code snippet:

  • The string containing special characters and an empty string is initialized, respectively.
  • After that, loop through each string character via the “for” loop.
  • The “isalnum()” and “isspace()” methods are used to check if the character is alphanumeric or whitespace. If so, the particular character is appended to the new string.

Output

In the above snippet, the special characters have been replaced with the help of a placed exception in the loop.

Conclusion

To terminate/remove special characters from a Python string, the String “replace()” method, the “join()” and “filter()” methods, the “Regular Expression”, or the “Looping” can be used. These approaches can easily remove special characters from a string and obtain a clean string that is ready for further processing. This write-up presented various ways to remove a special character from the string using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply