Q:

A basic python function to censor replace words in sentences

I am trying to build a simple function to censor a specific word in a sentence. For example "hello hello hi" to "***** ***** hi" if I feed censor("hello hello hi", "hello"). Assuming I will not receive punctuation and sentences with upper case letters or empty strings.
After researching online I understand there is simpler solution, for example:
def censor(text, word):
    return text.replace(word, "*" * len(word)) 

I still want to understand from a learning perspective, what I did wrong in the below more complicated code.
def censor(text,word):
    split_text = text.split()
    length = len(word)
    for item in split_text:
        if item == word:
            item = ("*" * length)
        else:
            item = item
    return " ".join(split_text)

A:

You probably see what you did wrong based on the comments. But you might not understand why (and based on your question I think you may be interested to know).
In python you can roughly divide the datatypes into 2 categories: mutable and immutable. The basic difference is that you can create references to mutable type variables whereas you can only pass around the values of immutable type variables. Refer to this question for examples.
In your case you are iterating through a list containing strings. Strings happen to be immutable. So when you change item it won't have any effect on the corresponding element in split_text.
If instead you were iterating through a list of lists, you would actually be able to change the list elements using the placeholder variable.
Illustrated with an example:
x = [["foo"],["foo","bar"]]
for i in x:
    if len(i) == 1:
        i[0] += "bar"
    else:
        i.append("foobar")
print(x) #[["foobar"],["foo","bar","foobar"]]  

Hope this helps! :)

