无法从长字符串中删除某些符号

SIM*_*SIM -1 python string symbols python-3.x

在过去的几个小时里,我一直在尝试用一根镜头从一根长串中踢出一些符号,但是我怎么也不知道如何删除它们.如果我去使用.replace()函数,它将是一个更丑陋的方法,因为符号的数量不止一个,并且函数变得过长而无法覆盖它们.任何替代方式删除它们将非常感激.

这是我试过的:

exmpstr = "Hi there Sam! Don't you know that Alex (the programmer) created something useful or & easy to control"

print(exmpstr.replace("'","").replace("(","").replace(")","").replace("&",""))
print(exmpstr.replace("['()&]","")) #I know it can't be any valid approach but I tried
Run Code Online (Sandbox Code Playgroud)

我想要踢出的是'()&这个字符串中的符号,而不是我尝试使用.replace()函数的方式.

nos*_*klo 8

您可以使用带有替换的for循环:

for ch in "['()&]":
    exmpstr = exmpstr.replace(ch, '')
Run Code Online (Sandbox Code Playgroud)

或者你可以使用正则表达式

import re
exmpstr = re.sub(r"[]['()&]", "", exmpstr)
Run Code Online (Sandbox Code Playgroud)