I have a list with elements that have unnecessary (non-alphanumeric) characters at the beginning or end of each string.
Ex.
\'cats--\'
You can use a regex expression. The method re.sub() will take three parameters:
Code:
import re
s = 'cats--'
output = re.sub("[^\\w]", "", s)
print output
Explanation:
"\\w" matches any alphanumeric character.[^x] will match any character that is not x