#with .find() method (super sloppy. System's answer was much better formatted)
def substring_between_letters(word, start, end):
answer = word[word.find(start) + 1 : word.find(end)]
if word.find(end) == -1:
return word
elif word.find(start) == -1:
return word
else:
return answer
#Without .find() method
# Write your substring_between_letters function here:
def substring_between_letters(word, start, end):
start_value = 0
end_value = 0
while start_value < len(word):
if word[start_value] != start:
start_value += 1
else:
break
while end_value > -len(word):
if word[end_value] != end:
end_value += -1
else:
break
if start_value == len(word):
return word
elif end_value == -len(word):
return word
else:
return word[start_value + 1: end_value]
# Uncomment these function calls to test your function:
print(substring_between_letters("apple", "p", "e"))
# should print "pl"
print(substring_between_letters("apple", "p", "c"))
# should print "apple"```
bleier
May 27, 2019, 11:25am
2
This is the mot simple version I could think of:
def substring_between_letters(word, start, end):
if start not in word or end not in word:
return word
else:
return word[word.find(start)+1:word.find(end)]
1 Like