. Advertisement .
..3..
. Advertisement .
..4..
Struggling with how to remove special characters from string Python? You just have landed on the right site then!
Here we not only provide you with the five most efficient ways to get your desired values but there are also samples to unwind your blocked thoughts.
Read on for further details!
Remove Special Characters From String Python
#1. Using The replace() Method
The first technique to come is employing the replace() feature. This approach utilizes a loop to locate specific emblems and then make a replacement with a blank sequence.
Running the code:
#define input string
sample_str = "$How %To @Remove# Special Characters] From! String&# #@Python"
#define peculiar symbol list
special_char = ['!','#','$','%', '&','@','[',']',']','_']
#print input sample string
print("Original String:", sample_str)
#using for loop and replace to detach special characters
for i in special_char:
sample_str = sample_str.replace(i,'')
# print final sample string
print("Final String:",sample_str)
Output:
Original String: $How %To @Remove# Special Characters] From! String&# #@Python
Final String: How To Remove Special Characters From String Python
#2. Using Regular Expression
Building a regular expression that will delete the distinctive symbol from the syllabary chain automatically is also a splendid path to take a trip on.
Have a look at the illustration below so you can grasp this math better. As such, [^a-zA-Z0-9] will be the frequent expression for this method, and ^ is what symbolizes every existing character, excluding the ones in brackets.
Running the code:
import re
text = "$How %To @Remove# Special Characters] From! String&# #@Python"
new_text = re.sub(r"[^a-zA-Z0-9]","",text)
print(new_text)
Output:
HowToRemoveSpecialCharactersFromStringPython
#3. Using The filter() Method
Arriving is the filter() method in Python to evacuate particular emblems from a sequence. We may use it in the same way we do with a “for loop“.
Running the code:
# Python program to separate all particular characters from string
# initialization string
sample_String = input('Lets enter any string: ')
# initializing special characters
special_Character = '@_!#$%^&*()<>?/\|}{~:;[]"'
# using filter() to remove special characters
new_string = ''.join((filter(lambda i: i not in special_Character, sample_String)))
# print string without special characters
print('New string:', new_string)
Ouput:
Lets enter any string: "$How %To @Remove# Special Characters] From! String&# #@Python"
New string: How To Remove Special Characters From String Python
#4. Using The str.isalnum() Method
Here goes another considerable technique to eliminate the specialized symbols from the series – the str.isalnum(). In employing such a method, we must first determine whether a character is alphanumeric and then discard it if it is not.
That way, the result returned will be True if the text in the strand is all alphanumeric which contains zero unuasul characters. Otherwise, it will revert back to False.
The following is an example of how to implement this method:
Running the code:
text_Sample = "$How %To @Remove# Special Characters] From! String&# #@Python"
new_text_Sample = ''.join(char for char in text_Sample if char.isalnum())
print(new_text_Sample)
Output:
HowToRemoveSpecialCharactersFromStringPython
#5. Using The translate() Method
Last but not least, the translate() function will be greatly beneficial in returning an order in which significant characters are substituted by those from a mapping table or a dictionary.
There is only one thing to bear in mind that it won’t work if the emblems are not specified in the table/dictionary. Hence, the earlier step in applying such an approach must be first to adopt the maketrans() function so that you can construct a mapping table.
After that, all that is left to do is merely filter a string by converting each particular char to an empty series.
Running the code:
import string
sample_String = "$How %To @Remove# Special Characters] From! String&# #@Python"
# Create translation table in which exceptional characters are mapped to empty string
special_Character = str.maketrans('', '', string.punctuation)
# Remove special characters from the string using translation table
sample_String = sample_String.translate(special_Character)
print(sample_String)
Output:
How To Remove Special Characters From String Python
In A Nutshell
The instructions on how to remove special characters from string Python are listed above. Hopefully, you’ve accumulated some helpful knowledge. Don’t forget to leave a comment if you have any unanswered questions!
2 Comments