I want to replace multiple substrings in a single string, and I want to know what approach is most efficient / best practice.
I've tried using str.replace() and it works but that seems inefficient. I'm running Python 3.6, so I'd like a solution compatible with that.
For some context here, I want to create the name of a (new) class from some text I read. So I need to convert text to (strings for) valid Python identifiers (e.g., "bad-Classname's" becomes "badClassnames"). So while my question of the best way to replace multiple substrings in a single string still stands, if there is a better way to convert text to class names I'd be happy to hear that too.
My existing code is as follows:
my_str = my_str.replace(" ", "").replace(",", "").replace("/", "").replace("'", "").replace("-", "").replace("’", "")
Is there a better way to do this? Regular expressions, a for loop, some builtin string method that I didn't know about?