0

I am trying to write a function that will convert a dedicated word within a string into asterisks. Basically, I want to censor a word from a string (ex. change "Hello World" into "Hello *****" if I made "World" the dedicated word). I tried to write the following code, but the code will not convert words into asterisks.

def censor(text, word):

    a = text.split()

    replace = ""

    for i in word:
        replace += "*"

    for i in a:
        if i == word:
            i = replace

    result =' '.join(a)

    return result

Can someone help me? Everything in the code seems to work except for the line i = replace.

Thanks!

3
  • 2
    That line works fine, it just doesn't do what you think it does. Commented Feb 26, 2018 at 2:07
  • 1
    More accurate question: Why can't you reassign a loop variable and have it persist? Commented Feb 26, 2018 at 2:11
  • Ah, I understand now! Thanks everyone! Commented Feb 26, 2018 at 2:13

4 Answers 4

2

i = replace rebinds the variable named i to the string in replace. It does not update the list as you seem to expect. You could fix your code by assigning replace to the item in the list using an index:

for idx, s in enumerate(a):
    if s == word:
        a[i] = replace

Now the item in the list a will be updated.

Sign up to request clarification or add additional context in comments.

Comments

2

If you are just trying to change a substring in a string, you can use something like this:

def censor(text, word):
    return text.replace(word, '*'*len(word))

This will replace all instances of word with enough *s for the len(word).


Just realizing the problem with my answer is that if you wanted to censor out "world" but not "worldbreaker", you would run into problems, cause you would end up with "*****breaker".

In this case, I would say to do something like:

censor(text, word):
    a = [w if w!=word else '*'*len(w) for w in text.split()]
    return ' '.join(a)

In the second line we let each w (a word from text.split()) remain unless it is word, in which case we replace it with *'s enough to fill. Then we join it with spaces and return

Comments

1

This might help

def censor(text, word):
    a = text.split()
    for i, v in enumerate(a):
        if v == word:
            a[i] = "*"*len(v)
    return  ' '.join(a)

print censor("Hello World", "World")

Output:

Hello *****

Comments

0

You can always use the find() method.

def replaceWord(word, sentence):
    # find the starting position
    posStart = sentence.find(word)

    # find the ending position
    posEnd = posStart + sentence[posStart:].find(" ") - 1

    # Get the word
    new_word = sentence[posStart:posEnd+1]

    # Turn word into a list
    list_word = list(new_word)

    # Find length of word
    word_length = len(sentence[posStart:posEnd + 1])

    # replace the word with "*"
    star = ""
    for i in list_word:
        star = star + i.replace(i, "*")

    # Add back in sentence
    new_sentence = sentence[0:posStart - 1] + " " + star + " " + sentence[posEnd +2:]
    return new_sentence

print(replaceWord("fox", "The fox is there"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.