10/15 censor


#1

This is my code. The result is thisisa****, I don't know how to convert it into this is a ****

def censor(text, word):
text = text.split(' ')
result = ''
for words in text:
print words
if words == word:
result += '*' * len(word)
else:
result += words
return result
print censor('this is a test', 'test')


#2

def censor(text, word):
    text = text.split()
    result = ''
    for words in text:
        print words
        if words == word:
            result += '*' * len(word)+" "
        else:
            result += words+" "
    return result.rstrip()
print censor('this is a test', 'test')


chnged

and


#3

Thank you! It works! But I don't understand .rstrip() what does this method do?


#4

@jinghua95,

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

print censor('this is a test', 'test')

#5

each time a word was appended to result a space was also a added at the end.This implies that the final value of result will have a space at the end.
That space must be removed and we can do so with the string.rstrip() method.
The rstrip(right strip) will remove all spaces at the right end of the desire string


#6

Thanks! Making result a list is better than a str


#7

not work for me :frowning:


#8
def censor(text,word):    
tex= text.split()
new_text=[]
for t in tex:
    if t==word:            
        t="*"*len(t)
    new_text.append(t)
return " ".join(new_text)

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


#9

Hi,

I am using this code..

def censor(text,word):
my_string=text.split()
for i in my_string :
my_string = str(my_string)
if word in text :
new_str = string.replace(my_string , word, "*" *\
len(word))
new_str=list(new_str)
return " ".join(new_str)

    else :
        return my_string

Error : It returns "[ ' * * * ' , ' * * * ' , ' * * * ' ]" when it should return "*** *** ***"

please help me in rectifying this error..


#10

My solution:

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

#12

Hi, I think my code works fine, but still I'm not allowed to continue :<

def censor(text, word):
split = text.split()
newword = ""

for i in range(len(split)):
    if split[i] == word:
        newword = newword + " " + "*" * len(split[i])

    else:
        newword = newword + " " + split[i]
return newword

print censor("hey hey hey", "hey")


#13

This is my code.

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

#14

I used similar code that everyone here was using, but was having a hard time with the error of having a space at the end of my returned string, and the code itself looking a little convoluted. Here's what I did today:

def censor(text,word):
ast = len(word) * "*"
return text.replace(word, ast)


#15

I didn't quite use the same method as what I've found above but I know I am just a little off with my code. Can anyone see what's up?
def censor(text, word):
numast= 0
ast= numast*'*'
string= text.split()
for i in word:
numast+=1
for word in string:
string= string.replace(word, ast)
cens=''.join(string)
return cens


#16

This bit of code seems to get the intended result. However it shows up as not correct. Can someone please explain to me if this is oversimplified or why this is not correct. Just have to modify the print text line with an indent to recreate.

def censor(text,word):
word = str(word)
letters = len(word)
asterisks = letters * "*"
if word in text:
--->print text.replace(word,asterisks)
return text
print text

censor("hey hey hey","hey")


#17

This seemed to do the trick. It doesnt include any join. i feel like its lazy but it seems to work regardless of the inputs.

def censor(text,word):
----word = str(word)
----letters = len(word)
----asterisks = letters * "*"
----if word in text:
--------text = text.replace(word,asterisks)
----return text

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


#18

Man I like to make mine complicated. I try to keep it as simple as I can but I basically forgot the key .append() method. Creating a separate empty list and just appending is nice a straightforward. In my attempt, I ended up finding the index, removing the index, and then re-inserting the new value. Took a while. :slightly_smiling:

def censor(text, word):
    tS = text.split()
    for i in tS:
        loc = tS.index(i)
        if i == word:
            tS.pop(loc)
            i = '*' * len(word)
            tS.insert(loc, i)
    return ' '.join(tS)
    
print censor('hey there man i hey you say', 'hey')

#19

Hi, I do it without "split" and "join". The code academy considers its correct.

def censor(text, word):
    new_text=""
    for i in text:
        new_text = text.replace(word, "*"*len(word))
    return new_text

#20

This a very clever solution! Props to you! :slightly_smiling:


#21

just returning this line an getting rid of everything else will do the trick