美文网首页
join和split的使用

join和split的使用

作者: sbill | 来源:发表于2017-04-07 16:16 被阅读0次

split 能把字符串分割成列表,join是把一些东西加进字符串里面

def censor(text, word):
    words = text.split(" ")
    print(words)
    w = []
    for i in words:
        if i == word:
            w.append(len(i)*"*")
        else: 
            w.append(i)
    print(w)
    print (" ".join(w))
    a = " ".join(w)
    print(type(a))
    return " ".join(w)

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

输出:

['this', 'hack', 'is', 'wack', 'hack']
['this', '****', 'is', 'wack', '****']
this **** is wack ****
<class 'str'>

相关文章

网友评论

      本文标题:join和split的使用

      本文链接:https://www.haomeiwen.com/subject/kyvhattx.html