Dark theme

Answer


We might consider str.index(); but this is worse than str.find(): it breaks the whole program when it can't find the text it is looking for. A much better option than both is str.replace(): this searches for text in a string and returns a copy of the whole string with the search text replaced with another piece of text; more importantly, although we can limit the number of replacements, if we don't it will replace every instance of the search text. This allows us to completely get rid of the current algorithm! Can you write a new version using this?

Here's a new version that does the job in a lovely Pythonic manner (test3.py): string_with_emoticons = "Oh, hai! Can I haz cheezeberger? :D :D"

emoticons = (":D",":)",":/",":p")

for emoticon in emoticons:
    string_with_emoticons = string_with_emoticons.replace(emoticon, "")

print(string_with_emoticons)

The documentation really helped us that time. However, this won't always be the case. Python documentation is notoriously mixed. Frequently it will tell you little about what is needed in the way of parameters, and/or what is returned from functions. Documentation often concentrates of what the library coders thing would be useful, rather than the whole API. Experimentation is key, but also using print(type(variable)) to explore what kind of thing is returned from functions. Each time you come across bad documentation, think "when I write my documentation, I'm going to put the time in to make it better than this!". Poor documentation means unusable code, and unusable code is pointless; documentation is therefore a key part of good code.