I tried the following boolean to achieve the return word if start or end are not in the string. It seems to always come back true. But if I remove the or condition and just use if start not in word then returns false and moves on.
def substring_between_letters(word,start,end):
if start or end not in word:
return print(word)
else:
startPoint = word.index(start)
endPoint = word.index(end)
return print(word[startPoint+1:endPoint])
#Uncomment these function calls to test your function:
print(substring_between_letters("apple", "p", "e"))
Why does the following code print “None” as well as the expected result?
def substring_between_letters(word, start, end):
start_point = word.find(start)
end_point = word.find(end)
print(start_point, end_point)
if start_point == -1 or end_point == -1:
return print(word)
else:
return print(word[start_point+1 : end_point])
# 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"
They are the same, except that str.find() returns -1 if the string is not found, and is only available for strings, while obj.index() throws an error if the argument is not found, and is available to lists and other subscriptable iterables.
not takes precedence over or, so the expression is parsed as: if start or (end not in word):, and assuming that start is anything with a truth value of True (i.e. is not False, 0 (zero), an empty container (list,string, tuple, dictionary), or any expression that evalates to one of those), it will further parse to if True or anything_else:, which will always return True.
The function print() evaluates the expression within its parentheses, and then sends the resulting value as a stream of text to the default i/o device, generally the screen. But (as with every Python function having no explicit return starement), it returns the value None.
Remember:
We print() values that we want to see
We return values that Python needs to use (to assign to a variable, perhaps, or to send to another function.)
in and not in are of equal precedence (note that the table in this link shows precedence ordered from least to greatest), so consequently the expression is parsed:
if (start not in word) or (end in word):
…and then evaluated from left to right.
So if start not in word returns True, evaluation ceases, and the if block is executed. If that expression returns False, then end in word is evaluated. If it returns True, the if condition returns True and the if block is executed. If end in word returns False, the if block is skipped.
Remember that the expression x or y will behave as you expect only if both x and y are expressions that are guaranteed to return True or False.
In general, is is best not to use or as a type of iterable (or a shopping list): if (x or y or z) in str: It is much safer to use if x in str or y in str or z in str:
( … all of the above: ditto with and.)
The problem with return print(word[start_point+1 : end_point])
… is that is evaluated like this: return print(something)
So to know what to return, Python must evaluate the expression following return: print(something).
That means that the expression something is evaluated, and its value returned to the function print().
That function does its job, and the value returned by something is sent to the screen as a stream of text. The function print() has executed (printed to the screen), but it returns None. That is its nature: it prints, it does not return (and, in Python, that means it returns None.)
So the expression return print(word[start_point+1 : end_point]) ultimately parses to
return None (although something is printed in the meantime).
def substring_between_letters(word,start,end):
if start in word and end in word:
substring = word[word.find(start):word.find(end)]
return substring
else:
return word
here was my code. It got said it was wrong because in a-p-p-l-e, it got the first ‘p’ and then returned ‘ppl’ . So, should my code return only what is in the middle of the start and end, without including start nor end?
I would change the title of this code challenge to “find letters within word.” Sub string implies an oppressive hierarchy in code lacking legal awareness.
please anyone advise why my code did not run as output expected?
def substring_between_letters(word, start, end):
if start and end in word:
start_ind = word.find(start)
end_ind = word.find(end)
new_word = word [start_ind+1:end_ind]
return new_word
else:
return word
The order in which Python evaluates this line is different from what you may think. in will be evaluated before and. This means you are essentially evaluating if start and (end in word):. Make sure you are checking if both start and end are in word.
def substring_between_letters(word, start, end):
if start in word and end in word:
substring_of_word = word[word.find(start) + 1:word.find(end)]
return substring_of_word
else:
return word
#I dont know why if start or end not in word
doesnt work (if someone can comment me why),
but this way it works properly
def substring_between_letters(word, start, end):
if start not in word:
return word
elif end not in word:
return word
else:
return word[word.find(start)+1:word.find(end)]
The problem is with operator precedence. or, not, and other operators in Python follow a set hierarchy. not has higher precedence than or. This basically means that not will be evaluated before or.
So, if your expression is if start or end not in word, it is evaluated as if start or (end not in word) (I’ve added the parentheses to show that not is evaluated first). Because start likely holds a non-empty string, it will evaluate to True (if it were an empty string, it would be False). This means that your expression will almost always be True since, if start is True, then the expression is satisfied as or only needs one of the two things it is comparing to be true.
Note: in future posts, please post your code formatted using the </> button.