How can I use split( ) to make censor?

.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)


      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:

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.


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.

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
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)
          text_censored = "".join(item)
    text_censored = "".join(text_censored)
    print text_censored
censor("this hack is wack hack", "hack")