How can I use split( ) to make censor?

n cannot be changed in a for..in loop. To modify a list we need access to its index.

1 Like

My method of using replace didn’t fix the issue of censoring part of a word, but it worked otherwise. Here it is:

def censor(text, word):
asterisks = len(word) * “*”
censored = text.replace(word, asterisks)
return censored

print censor(“is this going to work”, “is”)

image

Edit: Looking more closely at your code, there are a couple problems.
print text.replace (“word”, “*”, len(word)))

First of all, there are three closing ))) when there should be two, which gave an error. You also asked it to replace only the actual string “word” rather than the chosen input (in this case it should be the word “not”). Since “word” was not in your input sentence, no replacement could happen.

As a more nitpicky item, you put len(word) in as the number of times to replace ‘word’ when it is found in the string. As in “replace word with a single asterisk for len(word) number of times that the input word is found to occur in the string.” If your censored word occurs 30 times in the text, it will not be replaced in all 30 places, because you set a limit (based on the length of the word) for how many times the replace function should do the replacing. If the word is 5 characters long, it will only be replaced the first 5 times it appears in the text. In this case, you can simply leave out that third parameter of replace, because you want your input word to be replaced every time it occurs, which is the default setting of replace.

In order to have each letter of the input word replaced with an asterisk, you need to separately define how many asterisks to replace that word with.

Here is a new version of your code that will replace your chosen input word with the single asterisk that your code is requesting:

def censor (text, word):
print text.replace (word, “*”, len(word))

censor (“I do not like this exercise”, “not”)

image

Can someone help me? I don’t know what im doing wrong.

def censor(text, word):

text.split()

for word in text:

word = "*" * len(word)

" ".join(text)

return text

print censor(“this hack is wack hack”, “hack”)

.split() doesn’t modify the string, instead split returns a new list. Which you do nothing with

word (the iterable) is a read-only value, any changes made to word won’t persist

same problem with .join() as you had with split. .join() creates a new string, which is returned.

A post was split to a new topic: Split for censor

8 posts were split to a new topic: How can i use split() to make censor?

2 posts were split to a new topic: Censor - about replace method

A post was merged into an existing topic: Censor - about replace method

Instead of using count, I used the index() formula. My code does not work when there are most than one variable word in the text. I get the error " AttributeError: ‘str’ object has no attribute ‘remove’|’ ‘’

I can find the solution to this issue. Could someone help me ?

def censor(text, word):

  text_list = text.split()

  for variable in text_list:

    if variable == word:

      replacement_word_index = text_list.index(word)

      text_list.remove(word)

      asterisks =  len(word) * '*'

      text_list.insert(replacement_word_index, asterisks)

      text_list = ' '.join(text_list)

  return text_list

#here we have twice the variable test in our text, it works when there is only one
print censor('This test is a test ', ‘test’)

Thank you!

PS: apologies if the code is not in the right format in my post, the instruction link is broken: https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351

this happens inside the first iteration of your loop:

text_list = ' '.join(text_list)

so now, for the second iteration of the loop, text_list is a string. List methods like .remove() are not available for strings.

2 Likes

Quick question. I was noodling around at the beginning and, yes, I understand I was nowhere near finished, I wanted to test printing to the terminal. However, when printed, it returned additional text. Is this Code Academy being funny?

def censor(text,word):
  text = text.split(" ")
  for t in text:
    print t

censor("help me","no")

codecademy very likes calls your function for validation purposes/test cases.

so using print within the function, might result in additional output

Just wanted to share here. Because i found an easy way to solve this without any if-statements or for-/while-loop:

def censor(text, word):
  text_split = text.split(word)
  asterisks = "*" * len(word)
  return asterisks.join(text_split)

this code introduces an edge case:

print(censor('this is a test', 'is'))

now this gets partial censored, that might not always be desired?


My first attempt went immediately to the .replace() function. Reading the FAQ here, i modified my initial code to deal with the “is” trouble in “this”. What I still need to fix is the “This” issue at the beginning of the string, (my code won’t censor words that occur at the start of the string now, because it is looking for a blank in front. But that just gave me an idea. I’ll just add a blank to the front of the string!) I am thinking to grab the first X characters from the string, where X equals len(word to censor). I’m really against using split and join for some reason. I suppose I fear all the additional lines of code will invite a syntax mistake somewhere.
Cheers,
Higrm

def censor(text,word):
  if " "+word in text:
    return text.replace(" "+word," "+"*"*len(word))
  else: return text  
print (censor("this hack is wack hack", "is")  )

New and improved code now catches words at the start of the string too! I’m sure there are cases where it will fail, but it’s pretty good by my low standards.

def censor(text,word):
  if " "+word in " "+text:
    return (" "+text).replace(" "+word," "+"*"*len(word))
  else: return text  
print (censor("this hack is wack hack", "this")  )

One final tweak to remove that leading " " I added to get the first word of the string recognized:

def censor(text,word):
  if " "+word in " "+text:
    return ((" "+text).replace(" "+word," "+"*"*len(word))).strip()
  else: return text 
print (censor("this hack is wack hack", "this")  )

looks like you are chasing down a rabbit hole, if I look at the solution, its very difficult to understand.

1 Like

And echoes the first argument.

Here’s my code. Seems to me it’s a bit diffrent from what was presented so far (I think). As stetim94 mentioned in one of the post I tried to keep it close to normal English. Decided “result” to be a list not a string: btw “slowo” is “word” in Polish

def censor(text, word):
text_list = text.split( ) #its a list now
result = #creating empty list
stars = “*” * len(word) #how many stars
for slowo in text_list: #for every word in text_list that is a list -
if slowo == word:
slowo = stars #assigning it a new value
else:
slowo = slowo #keeping original value
result.append(slowo) #appending to a list
censored_text = " ".join(result) #list into string
return censored_text

Hi! I really dont understand why you have to use a count…
Also, my result is just “****” and I dont know why, its crazy :frowning:

def censor(text, word):
    text_words = text.split()
    text_censored = ''
    word_censored = '*' * len(word)
    for item in text_words:  # this should works for every text_words, right?
        if item == word:
         text_censored = "".join(word_censored)
        else:
          text_censored = "".join(item)
    text_censored = "".join(text_censored)
    print text_censored
  
censor("this hack is wack hack", "hack")

I used a different method!

                   ***My Code***

def censor(text, word):
mod = text.replace(word , “*” * len(word))
return mod