10/15 what's the problem?


#1

def censor(text,word):
m=text.split()
l=len(word)
for i in m:
if i==word:

        m[i]="*"*l
return " ".join(m)

print censor("this hack is wack hack", "hack")


#2

Here try this

def censor(text,word):
    m=text.split()
    l=len(word)
    for i in m:
        if i==word:
            m[m.index(i)]="*"*l
    return " ".join(m)


print censor("this hack is wack hack", "hack")

The issue here is when you start the for loop it creates an iterator over the elements in the sequence. Since the elements in the sequence are strings that iterator is also of type string and you can only access an element in a list by indices(which are integers) hence you get an error so the only way to do that is to get the index of the current element


#3

Thanks, rydan, that was exactly what I was looking for.

But still not 100% clear... What does (return " ".join(m)) do exactly? I did not include that in my original code, and it threw this error:

"hey hey hey" returned ['', '', ''] instead of " *** ***"

I guess .split() takes the input from a flat string of length .len() and splits it into a list of n elements, delimited by spaces by default (?). And then .join() outputs a single flat string with each of n elements delimited by a space, as defined by " ".join(). Is that right?


#4

@ferretey
Check out the code snippets here on the join and split methods

see if you can understand what they do


#5

Hi @ferretey,

The Python Documentation for the str. join() method tells us that:

str.join(iterable)
Return a string which is the concatenation of the strings in the iterable iterable. The separator between elements is the string providing this method.

This might looks like a mouthful but if we break it down into chunks, what its saying is that:

  • "Return a string"
    • This is quite simple, it will return a string as a result
  • "which is the concatenation of the strings in the iterable iterable."

    • It will add each item of the list we ask him to lookup in order to produce the string it will return
  • "The separator between elements is the string providing this method."

    • It needs to be called on a string (in the doc exemple they call .join() on string called str) and will use that string as a separator between all the items of the list as it adds them up

To better understand how something works, we can try changing its input and see how it affects the output. Take this code as an exemple:

# str.join(iterable)

m = ["ha", "ho", "ha"]    #This is our "iterable"
print " ".join(m)         # What's inside the double-quotes is equivalent to "str" and will be used as the seperator

it will return:

ha ho ha

and if we change what is in between the double-quotes before the .join method...

m = ["ha", "ho", "ha"]
print "-".join(m)

it returns:

ha-ho-ha

so basically we just found out that .join will take each item from the list and seperate it with what we put in the double-quotes, in the first exemple it was an empty space and in the second a dash.

Hope this helped!