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?
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:
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.
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])
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
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