Censor - I have spaces between my returned result, Help


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/1/4?curriculum_id=4f89dab3d788890003000096


I keep getting a space at the end, or between my asteriks, I feel like I'm so close to figuring this out, but can't do the last step. Any ideas?


def censor(text, word):
    a = text.split(" ")
    b = [ ]
    for words in a:
        if words == word:
            b += ("*"*len(word))
        else:
            b += words
            
    c = "".join(b)
    return c


#2

We cannot push elements onto a list using concatenation. Use .append()

b.append(words)

When defining an empty list, recommend you use, b = [] with no included white space.

Remember to re-insert the space character as a separator.

return " ".join(b)

#3

def censor(text, word):
    a = text.split(" ")
    b = [ ]
    for words in a:
        if words == word:
            b.append("*"*len(word))
        else:
            b += words
            
    return " ".join(b)

This still gives me an error:

Oops, try again. Your function fails on censor("Yo go fro yo go","go"). It returns "Y o ** f r o y o **" when it should return "Yo ** fro yo **". How do I remove spaces from this?


#4

Use .append() just like above. We cannot push elements onto a list with concatenation.

Recommend initialize an empty list with no added white space.

b = []

#5

text.split() intsead of text.split(" ")


#6

Are you suggesting that this is an error? If so, you would be wrong. Either syntax is correct, and the latter is most explicit.


#8

InputCode:

def censor(text, word):
    newtxt = text.split()
    newnewtxt=[]
    newesttxt=""
    m = len(word)
    for c in newtxt:
        if (c==word):
            newnewtxt.append("*" * m)
        else:
            newnewtxt.append(c)
    newesttxt = " ".join(newnewtxt)
    return newesttxt 
text = raw_input("Enter a string")
word = raw_input("Enter a word to be censored")

print censor(text,word)

Output:

Enter a string hi fukrs hi
Enter a word to be censored hi
** fukrs **
None

#9

Thanks, this solved it

Use .append() just like above. We cannot push elements onto a list with concatenation.


#10

Just to clarify, consider,

>>> [1,2,3,4,5] + [6,7,8,9,10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

We can concatenate lists.

>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + 11

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "int") to list
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + "str"

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list

#12