Censor

Write a function called censor that takes two strings, text and word , as input. It should return the text with the word you chose replaced with asterisks. For example:

why cant i do as below??

def censor (text, word):
for n in word:
word.replace(‘n’,’*’)
print text
return text

censor (‘what wassup’,‘what’)

Hi @yushan6714638042 - welcome to the forums.

It helps, especially with Python, if you use the code </> button to format your code. Otherwise we lose the whitespace.

I think it’ll help you to think about what you’re trying to do in a non-code sense, before trying to implement it in code.

The goal is: for a given string, find any instances of a given word and censor them

So:

  1. Get the string, and the word, and pass them to the censor function. (def censor(text, word))
  2. For each word in the sentence, check if it’s the target word. (A for and an if…)
  3. If it is, replace the word. (Write some code to do the swap.)

Your function doesn’t do anything to the sentence you’re given (the value text), so it will never change anything. :slight_smile:

If you can work out the steps you’ll have to follow to complete the exercise, you’ll be able to implement them fairly easily in Python.

2 Likes