去除字符串中的感叹号和问号,字符串不变

img


以上是图片和具体内容,关于如何才能做到去除字符串中的感叹号和问号,字符串不变。

import re

def remove_special(content):
    """去除!和?"""
    # 方式一:replace
    # return content.replace('?', '').replace('!', '')
    # 方式二:正则
    return re.sub(r'[?!]', '', content)

print(remove_special('python is such a fun!'))
print(remove_special('?18?  ? UIC!'))

有帮助的话,请点采纳该答案~

在不引用正则的情况下,可以 return ''.join([s for s in string1 if s not in '?!'])