10/15 - what is the role of count?

    words = text.split()
    result = ''
    stars = '*' * len(word)
    count = 0
    for i in words:
        if i == word:
            words[count] = stars
        count += 1
    result =' '.join(words)

    return result

i dint get what does that words[count] do?


we have a string we want to censor, so first we convert the string to a list, separating the sentence into words

then we need censor specific words, to achieve this, we need to update elements within the list. This is where count comes in, it acts as index, indexes we can use to manipulate a list, remember:

a_list = ['a', 'b', 'c']
a_list[0] = 'd'
print a_ilst

but if you use a for loop, you can also just use range() which produces a list of integers which so happen to match your indexes of your list, if done right


which element? Didnt get that?


things inside a list are called elements or values. In this case, the words of our sentence we want to censor

def censor(text, word):
    words = text.split()
    stars = '*' * len(word)
    for i in words:
        if i == word:
            words = words.replace(stars, word)
    words = ' '.join(words)


censor("hey just get lost", "lost")

whats wrong in this?


using .replace() is boring, it allows for a single line solutions.

.replace() is a method belonging to string, words variable is a list.


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