Remove Special Characters from String Python

In Python, it is challenging for programmers to remove unwanted/special characters from the input string. Sometimes, they want to remove the multiple characters, but a list of particular malicious characters. Python programming language provides various built-in methods/functions for this corresponding operation.

This guide will talk about the method for removing special characters from the string in Python.

How to Eliminate Unnecessary Characters from the Python’s String?

To delete the unwanted characters from an input string in Python, the below-stated methods are used:

How to Eliminate Unnecessary Characters from Python’s String Using the “isalnum()” Method?

The “isalnum()” method deletes the unwanted characters from a string in Python. It returns “True” when all the existing characters in the input string are alphabets or numbers. On the other hand, it will return a “False” value if any special character is found in the input string.

Syntax

Here is the general syntax of the “isalnum()” method:

input_string.isalnum()

 

The “isalnum()” method takes no arguments.

Example

First, create a variable string “input_string” and pass a string value that contains special characters. Then, initialize the “resultant_string” empty string variable for storing the output:

input_string = "Wel;Come To! L:inu@xHin*t Wo%rl^d!"
resultant_string = ' '

 

Then, call the “print()” function to view the provided strings as an output:

print("Original_string : " + input_string)

 

Use the “isalnum()” method inside the “for” loop and check the special characters from the provided input string one by one with the “if” condition. If the checked character is not special, then it will pass to the previously created empty string variable:

for char_num in input_string:
    if char_num.isalnum():
    resultant_string += char_num

 

To get the resultant string value, use the “print()” statement:

print("Resultant_string : " + resultant_string)

 

According to the below-given output, the resulting string contains no special characters:

How to Eliminate Special Characters from String in Python Using the “replace()” Method?

The “replace()” method can be utilized for removing unnecessary characters from the input string in Python. It is built-in functionality that is offered in Python programming and used for replacing all the occurrences of special characters in the provided substring.

Syntax

Now, check out the general syntax of the “replace()” method:

string.replace(character_to_ be_replaced, character_to_be_replaced_with)

 

In the above-listed syntax:

  • string” is the resultant string.
  • replace()” method replaces specific characters with the specified characters.

Example

First, initialize the special character list that needs to be replaced:

special_char = [';', '!', ':', '@', "*", '%', '^','!']

 

Next, call the “for” loop and replace the special characters with an empty string. Then, passes to the “resulatant_string” string type variable:

for i in special_char:
    resultant_string = resultant_string.replace(i, '')

 

Lastly, revoke the “print()” statement and display the resulting string:

print("Resultant_string : " + str(resultant_string))

 

Output

How to Delete Special Characters from String in Python Using the “translate()” Method?

In Python, another efficient way to eliminate the special characters from the provided input is using the “translate()” method. It uses a mapping table for changing all characters that exist in the table’s key positions with the character that exists in the table’s value position. The “translate()” method is utilized for replacing each special character with the empty string and getting the filtered string.

Syntax

The general syntax of the above-discussed method is provided below:

string.translate(table, character_to_be_removed)

 

Here:

  • table” is a mapping table created along with the “maketrans()” method.
  • character_to_be_removed” parameter is an option that indicates the characters to be removed from the specified input string that is being modified.

Example

Initially, import the “string” module:

import string

 

Next, create a dictionary table for the mapping table. Then, create the mapping table “table” with the “maketrans()” method and passes the created dictionary table as an argument to it:

table_dict = {sp_char : '' for sp_char in string.punctuation}
table = str.maketrans(table_dict)

 

Now, initialize call a “translate()” method that takes the mapping table variable as an argument and passes to the “resultant_string” string variable:

resultant_string = resultant_string.translate(table)

 

To view the resultant string, use the “print()” method:

print("Resultant_string : " + str(resultant_string))

 

It can be seen that the resultant string has no special character:

How to Delete Special Characters from String in Python Using the “filter()” Method?

By using the lambda function, the “filter()” method is used for deleting all the special characters and returning the desired refined resultant string. It is utilized for filtering an iterable according to the specified condition in the input string.

Syntax

Here is the syntax of the “filter()” method:

filter(lambda_fun, iterable)

 

Example

Use the “filter()” method along with the lambda function inside the “join()” method and pass to the “resultant_string” variable:

resultant_string = ''.join(filter(lambda i: i not in special_char, input_string))

 

To get the resultant string without unwanted characters, call the “print()” method:

print("Resultant_string : " + str(resultant_string))

 

Output

How to Eliminate Special Characters from String in Python Using the “re.sub()” Method?

To get the string without any special character in Python, the “re.sub()” method can also be utilized. The “re” regular expressions are used for identifying the special character from the provided string and the “re.sub” method replaces these unwanted string characters.

Syntax

First, check the general syntax of the “re.sub()” method:

re.sub(regex_pattren, replace_character, input_string)

 

In the above-specified syntax:

  • regex_pattren” is the user-defined regex pattern that is utilized for matching characters in an input string.
  • replace_character” is the character that developers will modify with the characters matching the regex pattern.
  • Input_string” is the specified string in which users search and replace characters.

Example

First, import the “re” package for using the regular expression:

import re

 

Now, call the “re.sub” method along with the respected arguments and then pass it to the “resultant_string” string variable:

resultant_string = re.sub(r"[^A-Za-z0-9]", "", input_string)

 

View the resultant string through the “print()” method:

print("Resultant_string : " + resultant_string)

 

That’s all! We have compiled multiple methods for removing unwanted characters from the provided string in Python.

Conclusion

To delete unwanted characters from the desired Python string, the “isalnum()”, the “replace()”, the “translate()”, the “filter()”, and the “re.sub()” methods are used. This post demonstrated the ways for removing special characters from the string Python.



from https://ift.tt/O2dikaJ

Post a Comment

0 Comments