how to avoid special characters in python code example

Example 1: remove special characters from string python

>>> string = "Special $#! characters   spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'

Example 2: python remove special characters from list

import re
my_list= ["on@3", "two#", "thre%e"]
print [re.sub('[^a-zA-Z0-9]+', '', _) for _ in my_list]

Example 3: remove special characters from string in python

''.join(i for i in string if i.isaplha()

Example 4: remove special characters from string python

import re
re.sub('[^A-Za-z0-9]+', '', mystring)

Tags:

Java Example