FAQ: Code Challenge: String Methods - Substring Between

I managed to use them in the same loop but got rid of the return and break statement. Thank you for the reply

By doing both in the same loop, you might have a bug for repeated characters. For example:

substring_between_letters("aabb", "a", "b")  # start at 0, stop at 2
1 Like

Ahh I realise my error when using this string. Thank you!

And now you might have another bug.

substring_between_letters("baabb", "a", "b")  # start at 1 stop at 3

…well, I guess it’s up to interpretation. but either way, there’s a conscious decision to be made here about how that should behave. I could argue that it should look for the stop position at the start position and therefore not stop at the first b, but I could also argue that it should stop at the first stop character found, or for that matter the last one.

My function works, but I feel like it is not efficient and will break at some point.

Could anyone suggest how I could improve/optimise it?

def substring_between_letters(word,start,end):
  if start and end not in word:
    return word
  start_pos = word.find(start)
  # print(start_pos)
  end_pos = word.find(end)
  # print(end_pos)
  return word[start_pos+1:end_pos]

What is it doing that it shouldn’t need to?
(You’d have to be able to say that to say it isn’t efficient, right? And, that would also be the answer to what improvement to make.)

Alternatively, describe the steps it SHOULD be carrying out to be correct and efficient, and then break those down into code.

1 Like

I see several solutions which are functionally the same, but they assign variables to the starting and end points. Does that in fact make the code more legible?

Here is my solution:

def substring_between_letters(word, start, end):
  if word.find(start) == -1 or word.find(end) == -1:
    return word
  else:
    return word[(word.find(start)+1):(word.find(end))]

Now i feel like i should give up already… Dont understand sh…

Can someone explain

  1. start_ind > -1 and end_ind > -1:
  2. word[start_ind+1:end_ind]
1 Like

The .index() and .find() methods both return -1 if a match is not found. That line likely checks against that condition.

That expression uses the slice method to create a string segment that starts on start_ind + 1 and goes up to but not including end_ind.

a = 'fellowship'

b = a[3:6]

print (b)    #  low

Eg.

     3 (included)
      \
 f e l l o w s h i p
      [l o w] \
               6 (excluded)

The topic of slicing comes up in the study of strings, and the study of lists. If you are finding some exercises a bit over one’s head then the thing to do is go back to the basic exercises and do them again for review.

2 Likes

wow thank you very much. I now get it.

1 Like

this works great, just don’t remember up to this point being shown find in the lessons, did i miss something?

I believe that the find method was not taught in the course. Given this, how could you solve this exercise without using find?

if word.count(word[start+1:end]) == 0:

return word

else:

return word.count(word[start+1:end])

getting the error that str must be an int, can anybody help?

I believe in both instances you should be returning a string. Calling the .count() method of word would return an integer. You’d want to alter the expression to return the relevant substring and not a count.

I think the code challenges should be explained as a step-by-step lessons first. The functions are not the issue, I understand them. The solution is very clear and I understand what is being done. But the process between, requested by the challenge, is a complete blank in my head.

1 Like

Welcome back to the forums!

I believe the code challenges are designed to test your knowledge from the previous lesson, which taught the concepts you will need to use to complete the challenges. Perhaps go back and review the lesson on string methods and try tackling the challenges afterwards.

that’s beautiful code. I love how you figured out to do in all so succinct.

1 Like

ohhh god i dont understand any of this…
what does this mean?

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

Like I am really confused about the -1 and +1 part… “If either of those indices are -1 , then the original string didn’t contain one of those characters, and you should return word .” why -1??

It’s always good to double check the documentation for functions and methods you’re unsure about- https://docs.python.org/3/library/stdtypes.html#str.find
Because array indexing starts at 0 in Python .find can’t return 0 for a standard False return so it uses -1 to indicate a negative response (where it cannot find the substring referenced by end in your example). Does the comparison make a little more sense now?

As for +1 consider how indexing works, if you’re not certain have a look at the difference when you include +1 and when you don’t.