15 / 10 Censor Easy Version


This should be a very straight forward version of this code to solve the problem for this question.


This section really gave me a loop around and I wanted to share a solution that was simpler than the ones I had found

I was getting frustrated with the solutions below this one that were not clear and concise and poorly commented

Cheers m8s

#Function Definition
def censor(text, word):
    #use a wordList var and set it = to the text.split with an empty space (string parsing)
    wordList = text.split(" ")
    #make a seq var to hold what ever word gets passed with = amount of stars
    seq = "*" * len(word)
    #loop through the length of our wordList
    for i in range(len(wordList)):
        #check to see if the wordList at i is == to the word we are checking for
        if wordList[i] == word:
            #replace the filtered word with our star sqeuence
            wordList[i] = seq
    #finally return  a string conjoined with our new filtered wordList
    return " ".join(wordList)


I want to afford an (from my point of view) easier version:

def censor(text, word):
    longi_texto = len(text)
    longi_pala = len(word)
    reemplazo = "*"
    if longi_texto>longi_pala:
        if word in text:
            texto = text.replace(word, reemplazo*longi_pala)
        return texto


Here's my version:

def censor(text,word):
    a = ""
    for i in range(len(word)):
        a += '*'
    text = text.replace(word, a);
    return text


I referred to your version to make it simple.

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