How do you remove unwanted characters from a string in python?

The generic problem faced by the programmers is remove unwanted characters from a string using Python. But sometimes the requirement is way above and demands the removal of more than 1 character, but a list of such malicious characters. These can be in the form of special characters for reconstructing valid passwords and many other applications possible. Let’s discuss certain ways to perform this particular task.

Method 1: Removing symbol from string using str.isalnum[]

Python String isalnum[] method checks whether all the characters in a given string are alphanumeric or not. It returns a boolean as True – If all the characters are alphanumeric or else false – If one or more characters are not alphanumeric.

Python3

string = "Ge;ek * s:fo ! r;Ge * e*k:s !"

test_str = ''.join[letter for letter in string if letter.isalnum[]]

print[test_str]

Output:

GeeksforGeeks

Method 2: Removing symbol from string using replace[] 

One can use str.replace[] inside a loop to check for a bad_char and then replace it with the empty string hence removing it. This is the most basic approach and inefficient on a performance point of view.

Python3

bad_chars = [';', ':', '!', "*", " "]

test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !"

print["Original String : " + test_string]

for i in bad_chars:

    test_string = test_string.replace[i, '']

print["Resultant list is : " + str[test_string]]

Output : 

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Method 3: Removing symbol from string using join[] + generator 

By using Python join[] we remake the string. In the generator function, we specify the logic to ignore the characters in bad_chars and hence construct a new string free from bad characters.

Python3

bad_chars = [';', ':', '!', "*", " "]

test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !"

print["Original String : " + test_string]

test_string = ''.join[i for i in test_string if not i in bad_chars]

print["Resultant list is : " + str[test_string]]

Output : 

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Method 4: Removing symbol from string using translate[] 

The most elegant way to perform this particular task, this method is basically used for achieving the solution to this kind of problems itself, we can translate each bad_char to an empty string and get the filtered string.

Python3

import string

bad_chars = [';', ':', '!', "*"]

test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !"

print["Original String : " + test_string]

delete_dict = {sp_character: '' for sp_character in string.punctuation}

delete_dict[' '] = ''

table = str.maketrans[delete_dict]

test_string = test_string.translate[table]

print["Resultant list is : " + str[test_string]]

Output : 

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Method 5: Removing symbol from string using filter[] 

This is yet another solution to perform this task. Using the lambda function, filter function can remove all the bad_chars and return the wanted refined string.

Python3

bad_chars = [';', ':', '!', "*"]

test_string = "Ge;ek*s:fo!r;Ge*e*k:s!"

print["Original String : " + test_string]

test_string = ''.join[[filter[lambda i: i not in bad_chars,

                              test_string]]]

print["Resultant list is : " + str[test_string]]

Output: 

Original String : Ge;ek*s:fo!r;Ge*e*k:s!
Resultant list is : GeeksforGeeks

Method 6: Removing symbol from string using re.sub[] function: 

Regular expressions are used to identify the bad character in the string and re.sub function is used to replace the bad_char from the string. 

Python3

import re

test_str = "Ge;ek * s:fo ! r;Ge * e*k:s !"

bad_char = [";", "!", "*", ":", " "]

print["The original string is : " + test_str]

temp = ''

for i in bad_char:

    temp += i

res = re.sub[rf'[{temp}]', '', test_str]

print["The strings after extra space removal : " + str[res]]

Output: 

The original string is : Ge;ek * s:fo ! r;Ge * e*k:s !
The strings after extra space removal : GeeksforGeeks

Method 6 : Using in ,not in operators

Python3

bad_chars = [';', ':', '!', "*", " "]

test_string = "Ge;ek * s:fo ! r;Ge * e*k:s !"

print ["Original String : " + test_string]

s=""

for i in test_string:

    if i not in bad_chars:

        s+=i

print ["Resultant list is : " + str[s]]

Output

Original String : Ge;ek * s:fo ! r;Ge * e*k:s !
Resultant list is : GeeksforGeeks


How do you remove unwanted text from a string in Python?

The most common way to remove a character from a string is with the replace[] method, but we can also utilize the translate[] method, and even replace one or more occurrences of a given character.

How do you remove unwanted characters from Python?

Using 'str. replace[] , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace[] method will replace all occurrences of the specific character mentioned.

How do I remove unwanted characters from a string?

Example of removing special characters using replaceAll[] method.
public class RemoveSpecialCharacterExample1..
public static void main[String args[]].
String str= "This#string%contains^special*characters&.";.
str = str.replaceAll["[^a-zA-Z0-9]", " "];.
System.out.println[str];.

How do I remove two characters from a string in Python?

To remove multiple characters from a string we can easily use the function str. replace and pass a parameter multiple characters. The String class [Str] provides a method to replace[old_str, new_str] to replace the sub-strings in a string. It replaces all the elements of the old sub-string with the new sub-string.

Chủ Đề