15 / 10 Censor Easy Version


#1


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


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096#


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)


#3

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

#4

Here's my version:

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

#5

I referred to your version to make it simple.

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