Hellow fellow coders,
I have a question about one of the string code challenge exercises in the Learn Python 3 course.
The task is the following: Write a function named substring_between_letters
that takes a string named word
, a single character named start
, and another character named end
. This function should return the substring between the first occurrence of start
and end
in word
. If start
or end
are not in word
, the function should return word
.
For example, substring_between_letters("apple", "p", "e")
should return "pl"
.
The solution is the below:
def substring_between_letters(word, start, end):
start_index = word.find(start)
end_index = word.find(end)
if start_index > -1 and end_index > -1:
return (word[start_index + 1 : end_index])
else:
return word
I cannot get my head around these two lines of code, particularly the -1 and +1 part:
if start_index > -1 and end_index > -1:
return (word[start_index + 1 : end_index])
Could someone dummy-explain that to me? Apologies if the question is too obvious.
Many thanks in advance everyone!