Posts

Showing posts with the label immutability

Remove specific characters from a string in Python

Image
Clash Royale CLAN TAG #URR8PPP Remove specific characters from a string in Python I'm trying to remove specific characters from a string using Python. This is the code I'm using right now. Unfortunately it appears to do nothing to the string. for char in line: if char in " ?.!/;:": line.replace(char,'') How do I do this properly? It's been over 5 years, but how about using the filter function and a Lambda Expression: filter(lambda ch: ch not in " ?.!/;:", line) . Pretty concise and efficient too, I think. Of course, it returns a new string that you'll have to assign a name to. – John Red Feb 6 '16 at 10:35 filter filter(lambda ch: ch not in " ?.!/;:", line) @JohnRed: Actually it returns an iterator that returns a list of characters but if you...