Should I use .find() or index?

A few questions on this one:

  1. Why use find instead of .index?

  2. 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"))
  1. 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"
  1. 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.

  2. 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.

  3. 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.)

1 Like

So basically it only evaluates one statement at a time. If I wrote

if start or end in word

Would it always evaluate to True?

I.e. ‘start’ is never evaluated as whether or it is in word.

For instance:

if start not in word or end not in word:

appears to work and I’m guessing:

if start in word or end in word: 

would also work.

but would the following code break because of the “not” priority?

if start not in word or end in word:

So this is because there are two print statements in the function one of which will always be false and thereby return None?

Thanks!!!

The second operand will not be evaluated unless start cannot be coerced to True.


1 or 0     => 1
'a' or ''  => a
{} or []   => [] 
[] or {}   => {}

Correct.

This would be a case for AND where both operands must satisfy their conditional.


Membership is not precluded by truthiness…

>>> x = [(), ()]
>>> () in x
True
>>> 

This is a gotcha in strings…

>>> '' in 'string'
True
>>> 

In fact there are seven empty strings in that string.

>>> 'string'.count('')
7
>>> 'string'.find('')
0
>>>

This suggests the first test might be to test against the alpha set.

start.isalpha()
end.isalpha()

It would make logical sense when the end string is not in the target for the return to begin at the start string, assuming it is in the target.

pple
1 Like

No!

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).

2 Likes

in the code

  stint = word.find(start)
  endint = word.find(end)
  if start in word and end in word:
    return word[stint+1, endint]
  return word

an error says “TypeError: string indices must be integers”
doesn’t find() return an integer?

I use .slice() function.

def substring_between_letters(word,start,end):
  if ((end in word) and (start in word)) == False:
    return word
  else:
    z=word.split(start,1)
    fah=z[1]
    mek=fah.split(end,1)
    return(mek[0])

My solution:

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

The nice thing about slices is that they either make sense, or they don’t. In any case it won’t matter. Python will give the sensible portion (sequence) if one can be got.

What that means is we can loosely validate the indices given in the slice using logical operators.

>>> def f(w, s, e):
    return w[w.find(s) + 1 : w.find(e) + 1 and w.find(e) or None]

>>> f('xabcy', 'x', 'y')
'abc'
>>> f('zabcy', 'x', 'y')
'zabc'
>>> f('zabcn', 'x', 'y')
'zabcn'
>>> f('abc', 'x', 'y')
'abc'
>>> 

This is really just a doodling, and may have edge cases. Testing required. On the surface, it looks to be quite solid.

Faulty slice indices will not raise an error (unless they are non-integer). They just might not work as expected.


Deconstructing

w.find(s) + 1

This value we already understand, or at least the example it is taken from would suggest as much.

s is found ? return index then add 1

We wish to exclude the exact index from the slice.

s is not found ? return minus 1 then add 1

We wish to include the first element in the slice.

Both cases generate a valid slice index that meets our needs.

w.find(e) + 1 and w.find(e) or None

A little more obscure, though discernible.

w.find(e) + 1

This operand will generate a non-zero value, or zero. That value is unimportant, only evaluated for truthiness. Not found is -1 so adding 1 will make that read as zero, which is falsy.

When the above is truthy, anding with the actual index will yield that index. If falsy, the yield is None.

[1, 2, 3, 4, 5][1:None]    #  [2, 3, 4, 5]

Clarification

yield

There are two ways to interpret that word. 1. to give way as in to allow another’s passage while we wait; or, 2. to generate as in crop yield.

Boolean expressions yield according to the second definition.

2 Likes