Stuck on a practice question

I have been posed the following question as practice for my course and I have no idea at all how to do it. Any help would be appreciated!

Write a function which takes two arguments, a string and a target letter. Determine and return the
encrypted word. The encrypted word is made up of the characters after each target letter, in order.
Here are the doctests:

>>> decode("I want no more to endure the names I am called by many people", 'n')
>>> decode("Hungry hares in lairs","r")
>>> decode("Turn around dear","r")

Find the letter and if the next character is a letter, add it to the result.

The target letter can occur more than once.

Well, let’s think about the steps that were described. Do you know how to look for the target letter in the string, and know each place that it appears? For any given place that it appears, do you know how to figure out what the next character is? If you have found all those “next characters”, do you know how to put them together? If you do all these steps, that should solve the problem, right? Where exactly do you get stuck?

For example, the test showed you that if the input string is "I want no more to endure the names I am called by many people" and the input target is 'n', then the result should be 'today'. Do you understand this result? Can you figure it out with pencil and paper? Can you explain how you do that?

1 Like

The initial problem that is inhibiting my efforts is that I am struggling to find all of the instances the target letter appears, rather than just the first or last one. Any suggestions?

Does this help?

(Here’s another idea: what if you started by splitting the string where the target letter appears?)

That split method worked a charm! Here is the way I did it (may not be the most glamorous but I don’t care it works). Thank you so much for the help!!

def decode(words, letter):
    for word in splitwords:
        if len(word)>=1 or len(word)==1:



What should decode("ook","o") return? You return "".

It passed the doctests I was given.
That’s all that matters to me.

Oh, I didn’t think of that when I suggested it :slight_smile:

Also, in letters+=word[0], every time the string in letters gets discarded and a new one with the appended letter gets created. You could consider collecting the letters first, for example in a list, and later joining them all together. For this, initialize letters=[], instead of letters="". To join, you can do "".join(letters).