Python censor

write a function called censor that takes two strings,text and word ,as input.It should return the text with the word you chose replaced with asterisks.
The number of asterisks you put should correspond to the number of letters in the censored word.
Hint:
remember:
""4 equals"****"
string.split()
" ".join(list)
after splitting the string with string.split() ,you can loop through the indices in the list and replace the word you are looking for with their asterisk equivalent.join the list at the end to get your sentence!

def censor(text,word):
f = text.split()
f =[w.replace(word,"*" * len(word)) for w in f]
return " ".join(f)

def censor(text,word):
return text.replace(word,"*" * len(word))

split没学,是在问答里找到答案的

str.split(str=" ",num=string.count(str))[n]
str 分隔符,默认为所有的空字符,包括空格,换行,制表符等
num 分割次数
[n] 表示选取第n个分片

你可能感兴趣的:(Python censor)