Can someone please explain this code

def censor(text, word):
    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

Explaining code from scratch is a very time consuming process, do you have specific questions? Have you tried inserting a function call and print statements so you can see what your code is doing?

count seems a bit redundant.

i only want to know what the split,result anfd the join modules are doing…

and this part

words[count] = stars

but if you wanted to know what built-in functions like split and join do, wouldn’t it be faster to find the documentation:

5. Built-in Types — Python 2.7.18 documentation
5. Built-in Types — Python 2.7.18 documentation

result = '' is doing much, it doesn’t add value to the program.

now i see why you use count, little bit too much used to seeing range in the for loop used then instead

For next time, put this in the topic directly so we know what to focus on.

This updates an element in your words list with asterisks, causing your list to become censored. Which you then .join() into a string after the loop

have you tried this? Please acknowledge.

1 Like

thanks got it