I need help with first part of project

I’m doing the censor dispenser project and it asked you to censor “learning algorithms” from an email. If I understand it correctly, how do I remove a piece of string from a string? I’ve tried this already:

def censor(email):
  if "learning algorithms" in email:
    censored = email.strip("learning algorithms")
  return censored
print(censor(email_one))

https://www.codecademy.com/practice/projects/censor-dispenser

Consider,

>>> help(str.strip)
Help on method_descriptor:

strip(self, chars=None, /)
    Return a copy of the string with leading and trailing whitespace removed.
    
    If chars is given and not None, remove characters in chars instead.

>>> 

Its been a while! From what I can tell I’ve met the lowest condition. Does it strip specific words? I think so

1 Like

Only if they are on the end (leading and/or trailing).

>>> email = "learning algorithms"
>>> x = email.strip("learning algorithms")
>>> x
''
>>> email = "123123123learning algorithms123123123"
>>> x = email.strip("learning algorithms")
>>> x
'123123123learning algorithms123123123'
>>> email.strip("123")
'learning algorithms'
>>>

For removing content anywhere in a string we do have tools intended for that purpose. For instance,

>>> help(str.replace)
Help on method_descriptor:

replace(self, old, new, count=-1, /)
    Return a copy with all occurrences of substring old replaced by new.
    
      count
        Maximum number of occurrences to replace.
        -1 (the default value) means replace all occurrences.
    
    If the optional argument count is given, only the first count occurrences are
    replaced.

>>> 
1 Like

Some overkill silliness for mechanics demo…

>>> email = "123123123learning algorithms123123123"
>>> x = email.index("learning algorithms")
>>> n = len("learning algorithms")
>>> s = list(email)
>>> s[x:x+n] = []
>>> ''.join(s)
'123123123123123123'
>>> 
1 Like

Do know if I can delete multiple words at once?:

proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
def censor2(email, pt):
  for el in pt:
    censored_one = email.replace( el, "**censored**")
    return censored_one
print(censor2(email_two, proprietary_terms ))

This doesnt function right. I was hoping to get rid of every words from the proprietary terms list

Yes. If you read the help info above you can remove ALL, or a specified number (optional).

On the other hand, if they are not adjacent so that they can be phrased, no. It would take two or more passes depending upon how many words.

if you consider what str.strip or str.replace does, then you’ll find that this is redundant. they already do that. try not to invent special cases where none exist

use print and compare results to what you meant

Sagely wisdom; word to the wise.

In any case, consider not using any of replace/find/index, instead cut up the string at each word boundary so that you have words and non-words separately, then you can censor the words separately one at a time, it would no longer be a big string, it would be a list of words. The things you’re asked to do has to do with words, so the first thing you’d want to do is therefore to extract the words. Doing logic inside the string would be a bit silly.

> groupOn isAlpha "some words here, separated by ... other things. \nnewlines\n, punctuation and what not, doesn't matter. get the words out."
["some"," ","words"," ","here",", ","separated"," ","by"," ... ","other"," ","things",". \n","newlines","\n, ","punctuation"," ","and"," ","what"," ","not",", "
,"doesn","'","t"," ","matter",". ","get"," ","the"," ","words"," ","out","."]