Python的字符串处理函数split & join的使用一例

来源:互联网 发布:cocos js button 隐藏 编辑:程序博客网 时间:2024/06/07 13:32
 如果要用Python实现这样一个函数:在一个字符串text 中查找 word ,并替换成 “*”

For example:

 censor("this hack is wack hack", "hack") 

should return

  "this **** is wack ****"

只需要一行就可以搞定:

def censor(test,word):    return (len(word) * "*").join(test.split(word))

验证效果:
输入

     print censor("hello,neo!","neo")
输出hello,***!

非常方便!

0 0
原创粉丝点击