Why do I have a syntax error?

def substring_between_letters(word, start, end):
mystring = [word.find(start)-1:word.find(end)]:
return mystring
I think this should work but im getting a syntax error.

This worked just fine for me:

Write your substring_between_letters function here:

def substring_between_letters(word, start, end):
s = word.find(start) + 1
e = word.find(end)
if s < 0 or e < 0:
return word
return word[s:e]

Uncomment these function calls to test your tip function:

print(substring_between_letters(“apple”, “p”, “e”))

should print “pl”

print(substring_between_letters(“apple”, “p”, “c”))

should print “apple”

Could someone show me this function in a different way?
This is the solution and I don’t understand why it returns “start_ind+1” and it’s not adding +1 at the end.

def substring_between_letters(word, start, end):
start_ind = word.find(start)
end_ind = word.find(end)
if start_ind > -1 and end_ind > -1:
return(word[start_ind+1:end_ind])
return word

You’re looking to get the substring in between the letters (non-inclusive). For example, if I called substring_between_letters("hello there", "e", "r"), I would expect a return value of "llo the" (note that the e in hello and the r in there aren’t included in the returned substring).

Now take a look at this line:

Based on your knowledge of list indexing, why do you think the code is written this way?

1 Like

I was able to get it now!! Thanks for your explanation.

1 Like