Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.
试试String.translate。
https://docs.python.org/2/library/string.html#string.Template.substitute
或者如果您使用的是新式的Python 3:
函数^{} 不改变
c
字符串本身(wrt toc
您调用的)只返回一个新字符串,旧字符串的出现已被新字符串替换。因此newstr
只包含一个被c
字符串中最后一个元音替换的字符串,即o
,因此得到的"Hey lk wrds"
与"Hey look words".replace('o', '')
相同。我认为您可以简单地将
anti_vowel(c)
写成:我所做的是遍历字符串,如果一个字母不是元音,那么只将它包含在列表(过滤器)中。过滤之后,我以字符串的形式返回列表。
你为什么不用regexp呢?根据documentation的说法,类似这样的操作应该有效:
如果经常使用该函数,可以编译regexp并使用编译后的版本。
相关问题 更多 >
编程相关推荐