Censor - Space problem


#1



HI

Please can I get some help with the spacing either at the beginning or end of the "text" sentence which I am monitoring for a specific word to amend.

It is very frustrating as I have tried several combinations - I have placed the + " " part of the code in line 6 both at the end and the beginning and it still gives the problem that "Hey Hey Hey!" is being deleted correctly but with one space before or after the phrase depending on where I put the + " ". I tried using the " ".join("*" * len(word)) code and that also gives a similiar error for "Hey Hey Hey!".


Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***".


Replace this line with your code. 
def censor(text, word):
    split_up = text.split()
    other_word = ""
    for data in split_up:
        if data == word:
            other_word += "*" * len(word) + " "
        else:
            other_word += data
    return other_word


#2

if you want to .join(), its recommended to make other_word a list, so that after appending asterisks and word that doesn't need censoring to list, you can join the list into a string and return it

you learned string slicing (and used it in your pyglatin program), you could use it here to overcome your problem

ideally, lists are recommend because they are mutable while strings are not


#3

Thank you Stetim94

I get what you saying about the slicing of the space. I don't think I did it right though - the previous error about the empty space behind the "hey hey hey" is not showing anymore but the entire word is not censoring now?

Here is the code from previously with the extra line I added:

def censor(text, word):
split_up = text.split()
other_word = ""
for data in split_up:
if data == word:
other_word += "*" * len(word) + " "
sliced_word = other_word[0:len(word) - 1]
else:
other_word += data
return sliced_word


#4

you should slice after the loop? You only need to remove the trailing space at the very end of the censored sentence


#5

Thank you, kind Sir! Finally solved it, sigh...


#6