This program will remove special characters from string python. If the string was named "Know@Program", then the string's result will be "KnowProgram". We'll discuss how to remove special characters from a given string using Regular Expressions, Replace(), Translate(), Join(), Filter() and Str.isalnum() functions.
First, we will import Regular expression (RegEx module).. The regular expression will remove all special characters from the string. [aZA-Z0-9] will be the regular expression. is any character that is not in brackets.
import re
string = input('Enter any string: ')
new_string = re.sub(r'[^a-zA-Z0-9]','',string)
print('New string:', new_string)
Output:
Enter any string: Test&[88]%%$$$#$%-+String
New string: Test88String
The Replace() method can be used to replace any substrings. This is an integrated functionality in Python. It replaces every occurrence of the old substring by the new substring. You can use replace() within a loop to search for special_chars and then replace them with empty strings, thus removing them.
string = input('Enter any string: ')
special_char = '@_!#$%^&*()<>?/\|}{~:;[]'
for i in special_char:
string = string.replace(i, '')
print('New string:', string)
Output:
Enter any string: itt^ut#ori@a
New string: ittutoria
To remove special characters, we are using the Join() method. The generator function specifies the logic to ignore special_char characters and construct a string that is free of special characters.
string = input('Enter any string: ')
special_char = '@_!#$%^&*()<>?/\|}{~:;[]'
new_string = ''.join(x for x in string if not x in special_char)
print('New string:', new_string)
Output:
Enter any string: join(special_char)
New string: joinspecialchar
The join() method was used in the previous program. In this program we use the join() and filter() functions to remove special characters from strings. The filter() creates an iterator using elements of an iterable, for which a function returns true.
string = input('Enter any string: ')
special_char = '@_!#$%^&*()<>?/\|}{~:;[]'
new_string = ''.join((filter(lambda i: i not in special_char, string)))
print('New string:', new_string)
Output:
Enter any string: join(filter(lambda:)
New string: joinfilterlambda
The same task is performed by the python program, but in different ways. This program also uses the str.isalnum() function. The str.isalnum() method returns True when the characters are alphanumeric characters. This means that there are no special characters in this string. If there are special characters in the string, it will return False.
string = input('Enter any string: ')
new_string = ''.join(filter(str.isalnum, string))
print('New string:', new_string)
Output:
Enter any string: join(filter(str.isalnum, string))
New string: joinfilterstrisalnumstring
The transform() method returns strings where certain characters are replaced by the character in a dictionary or in a map table. To create a map table, use the maketrans() method. The dictionary/table will not replace any character that isn't specified. Each special_char can be translated to an empty string, and a filter string created.
import string
test_string = input('Enter any string: ')
new_string = test_string.translate(str.maketrans('', '', string.punctuation))
print('New string:', new_string)
Output:
Enter any string: #string.translate()
New string: stringtranslate
Share this post with your friends if you enjoyed it. Are you interested in sharing more information on the topic or correcting any errors? Also, see more https://ittutoria.net/how-to-remove-special-characters-from-string-python/