【发布时间】:2019-08-25 02:04:29
【问题描述】:
我们正在计算机科学课上研究 Vigenere 密码,老师希望我们采取的第一步是删除字符串中的所有空格、标点符号和大写字母。
#pre-process - removing spaces, punctuation, and capitalization
def pre_process(s):
str = s.lower()
s = (str.replace(" ", "") + str.replace("!?'.", ""))
return s
print(pre_process("We're having a surprise birthday party for Eve!"))
我想要的输出是"werehavingasurpisebirthdaypartyforeve",但我实际得到的是"we'rehavingasurprisebirthdaypartyforeve!we're having a surprise birthday party for eve!"
【问题讨论】:
-
str.replace("!?'.", "")看起来替换了确切的模式!?'.。请参阅this post 了解如何替换多个字符。