FAQ: Code Challenge: String Methods - Substring Between

2 posts were split to a new topic: Why doesn’t my code work?

A post was split to a new topic: Integer isn’t iterable?

2 posts were split to a new topic: How to handle that a start and end in the wrong order?

2 posts were split to a new topic: Sharing solution

5 posts were split to a new topic: Should I use .find() or index?

3 posts were merged into an existing topic: How to handle that a start and end in the wrong order?

I don’t get the logic of hint we’re given:

If either of those indices are -1 , then the original string didn’t contain one of those characters, and you should return word .

If it returns -1 it means that on of those characters are last character in a string so why do they say that in that case original string did not contain that character?

1 Like

Why would that mean the last character, it’s not a location, there’s nothing to the left of 0
the last element is found at length - 1
If the function says -1 means something specific, why would it not mean that?

You have some function.
Read what it promises to do.
Leverage that to get what you want.

Here is my code:
def substring_between_letters(word, start, end):
first = word.find(start)
last = word.find(end)
if first or last == -1:
return word
else:
return word[first+1:end]

Uncomment these function calls to test your function:

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

I don’t know why either ‘first’ or ‘last’ is not equal to -1, but the system still keep to next step.

2 Likes

If you wish to make two comparisons then you would need to use == twice, you only use it once, so there’s only one comparison

to compare both a and b to c, you would need to carry both of these out:

compare(a, c)
compare(b, c)

you can then combine those results, for example you might want to know if both are equal, either, or none, or that one is equal but not the other.


(never write something and hope it does what you meant, that’s backwards. instead look at what something promises to do, and leverage that to get what you want)

thank you.
(never write something and hope it does what you meant, that’s backwards. instead look at what something promises to do, and leverage that to get what you want)
so that means I need to be very familiar with the use of every syntax.

I used your work, then just fixed some of it.
1 problem you had was the double comparison
2 if you slick’s it gotta be int
thanks

1 Like

Can someone explain why we have to use > and also why we have to use +1 when slicing the word?
if start_ind > -1 and end_ind > -1:
return(word[start_ind+1:end_ind])

My solution…it works

1 Like

Here is my solution :

def substring_between_letters(word, start, end):
  strt = word.find(start)
  nd = word.find(end)
  asd = word[strt+1:nd]
  if start and end in word:
    return asd
  else:
    return word
1 Like

Can’t figure out what is wrong with the code. Trying an alternative for string.find()

When I try to print the variables indA, indB or string it seems to work. Two letters and a word are inputted, and if the letters are present in the word, the characters between the two letters are printed.

def substring_between_letters(word, start, end):
  for index in range(len(word)):

    if word[index] == start:
      indA = index
      break
      return indA

    if word[index] == end:
      indB = index
      break
      return indB
      string = word[indA + 1 : indB]

    else:
      string = word

  return string

If you’re trying to both break and return in succession then you probably don’t understand what either of those things do

also you might want to not bundle everything into a single loop, but instead first find the start then find the end and then present the result

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