Can someone please explain this code


#1

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

#2

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.


#3

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

and this part


words[count] = stars

#4

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

https://docs.python.org/2/library/stdtypes.html#str.split
https://docs.python.org/2/library/stdtypes.html#str.join

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.


#5

thanks got it