Cesnor


#1



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):
    b=text.split()
    result=""
    for i in b:
        if i==word:
            i="*"*len(word)
        result+=i
        result+=" "
    return result    
how can i change my code in order to become correct??


#2

One option is to build a list instead of a string, then join the return value.

This is not covered in the course, but Python has a tool for removing white space...

string.strip()

#3

what do you mean "join"??


#4

Have you not yet learned about the .join method?


#5

sorry i found it
def censor(text,word):
b=text.split()
z=[]
for i in b:
if i==word:
i=""len(word)
z.append(i)
result=" ".join(z)
return result


#6

thank you again!! i appreciate it!!


#7

Aside

String.join(list)

is a method to join elements in a list (of strings) to form a single string.

>>> '_'.join(['1','2','3','4'])
'1_2_3_4'
>>>

It can also be used on tuples,

>>> '_'.join(('1','2','3','4'))
'1_2_3_4'
>>>

and on sets,

>>> '_'.join({'1','2','3','4'})
'3_2_1_4'
>>>

Notice that sets have no defined order.

String.join(string)

Given a string, we can use the above to insert separator characters.

>>> '_'.join('supercalifragilisticexpialidocious')
's_u_p_e_r_c_a_l_i_f_r_a_g_i_l_i_s_t_i_c_e_x_p_i_a_l_i_d_o_c_i_o_u_s'
>>>

#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.