FAQ: Code Challenge: String Methods - Substring Between

#1

This community-built FAQ covers the “Substring Between” exercise from the lesson “Code Challenge: String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Substring Between

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

#2

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.

#3

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”

#4

Curious why my code didn’t work. I must be missing something in the fun. Could not solve for ‘pl’

def substring_between_letters(word, start, end):
  if word.find(start) < 0 or word.find(end) < 0:
    return word
  else:
    word[word.find(start)+1:word.find(end)]
#5

Why not just use:

You also forgot the return in your else.

#6

def substring_between_letters(word,start,end):
the_list =
the_end_list =
for i in range(0,len(word)):
if(word [i] == start):
the_list.append(i)
for j in range(len(word)-1,-1,-1):
if(word [j] == end):
the_list.append(j)
return word[the_list[0]:the_list[-1]]
I get a warning that int object is not subscriptable. I am wondering Why cannot I acces my list in the function

#7

How should our function handle other cases, like where the “start” character appears after the “end character”?

For example, should this code:

substring_between_letters(word = "adogb", start  = "b", end = "a")

return “dog”, or simply return the word “adogb”

Another case is if the two arguments appear in the proper order, but only after they appear in the wrong order, like

substring_between_letters(word = "adogbruna", start = "b", end = "a"

Should this print “dog”, “run”, or the word?

#8

Substring Between

print (substring_between_letters("adogbruna", "b", "a"))
# adogbruna
print (substring_between_letters("dogbruna", "b", "a"))
# run

The first example returns the full word because a comes before b, and we cannot have the end at the beginning.

The second example we remove the starting a, and get the result we expect since a is at the end, and b does precede it.

Similarly, your first example should also return the full word. Switch the a and b around and it will return dog.

1 Like
#9
#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"```