It returns just the string without the new asteriks

In my code I expected storing the asterisks in i would change the i in string to equal the new value being the asterisks. I'm not sure how to join the i back into string gracefully

def censor(text, word):
    result = ""
    string = text.split()
    a = "*"
    for i in string:
        if i == word:
            i = (a*len(word))
            result = " ".join(string)
    return result


this won't work. You should join the list into a string after the loop (indention determines what is nested in the loop)

but you have a bigger problem, i contains read only values from the list, you can't use it to modify string, so no changes will persist after the loop

to manipulate lists, we need indexes, which function do we know that gives us indexes?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.