Censor question!


hey im working on the "censor" challenge. i came up with a weird approach: it's a series of nested loops that start at i in "text" and tests to see if it matches with "word" for the length of "word". if it does, it then replaces all those letters with stars.

(after clicking the hint, i have a better idea of what i'm supposed to do. but i'd like to get some feedback on why what i tried the first time didn't work!)

it's giving me a ""'str' object does not support item assignment" error".


def censor(text, word):
    for i in range(0,(len(text)-len(word))):
        match_counter = 0
        for x in range(0,len(word)):
            if text[i+x] == word[x]:
                match_counter = match_counter + 1
            if match_counter == len(word):
                for n in range(0,(len(word) - 1)):
                    text[i+n] = '*'
    return text


A string is immutable so this step cannot take place.


ahhh ok

and if i converted the whole text to a list i would lose the spaces right?


If you use list() it will preserve the spaces but the list will be of characters. If you use split(' ') it will return a list of words with no spaces.


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