1

find('asdf','') finds an empty string in 'asdf' hence it returns 0. Similarly, find('asdf','',3) starts to search for the string at index position 3 and hence return 3. Since the last index is 3, find('asdf','',4) should return -1 but it returns 4 and starts to return -1 only if the starting index is more than or equal to (last_index)+2. Why is this so?

6
  • possible duplicate of Find method with empty string Commented Nov 23, 2013 at 19:28
  • I don't think this is a duplicate of stackoverflow.com/questions/10392054/… Actually that question shows bahaviour exactly opposite to that in my question. Commented Nov 23, 2013 at 19:37
  • The answer to that question is the answer to your question, namely: "The last position is after the last character of the string" Commented Nov 23, 2013 at 19:55
  • @BrenBam yes I read that but the problem is if I do k='asdf' and then do k[4] that is just after the last index of the string, I get a out of range error. Commented Nov 23, 2013 at 19:58
  • Try doing k[4:4]. Since you are searching for a zero-length string, you should take a zero-length slice. Commented Nov 23, 2013 at 20:02

2 Answers 2

1

Because "asdf" without its first four characters still does contain "". A harder check comes into play when the index exceeds the length of the string, but having an index equal to the string is equivalent to "".find().

Sign up to request clarification or add additional context in comments.

2 Comments

If I need to find out the index positions where a certain substring occurs, and I send to my user defined function, the parent string and a null string, it returns index positions one more than it should. In this case, do I need to specifically check for null string and can't really have general way of getting the correct answer with the find function?
Another question, I'm not really aware of the ''.find() method. I'm pretty new to python. How is it that 'asdf' without the 4 characters still has a '' in it but after the 5th index position doesn't have a ''? What exaclty does 'hard check' mean?
1

Here is how it works:

0 a 1 s 2 d 3 f 4

When you use 'asdf'.find(sub), it searches those five positions, labeled 0, 1, 2, 3, and 4. Those five. Those five. No more and no less. It returns the first one where 'asdf'[pos:pos+len(sub)] == sub. If you include the start argument to find, it starts at that position. That position. Not one less, not one more. If you give a start position greater than the greatest number in the list of positions, it returns -1.

In other words, the answer is what I already repeated in a comment, quoting another question answer:

The last position [for find] is after the last character of the string

Edit: it seems your fundamental misunderstanding relates to the notion of "positions" in a string. find is not returning positions which you are expected to access as individual units. Even if it returns 4, that doesn't mean the empty string is "at" position 4. find returns slice starts. You are supposed to slice the string starting at the given position.

So when you do 'asdf'.find('', 4), it starts at position 4. It finds the empty string there because 'asdf'[4:4+len('')]==''. It returns 4. That is how it works.

It is not intended that str.find has a one-to-one mapping between valid indexes into the actual string. Yes, you can do tons of other indexing like 'asdf'[100:300]. That is not relevant for find. What you know from find is that 'asdf'[pos:pos+len(sub)] == sub. You do not know that every possible index that would return '' will be returned by find, nor are you guaranteed that the number returned by find is a valid index into the string if you searched for an empty string.

If you have an actual question about some use of this functionality, then go ahead and ask that as a separate question. But you seem to already know how find works, so it's not clear what you're hoping to gain from this question.

3 Comments

Thanks for the details. 0 a 1 s 2 d 3 f 4 do you mean that a '' is mapped to position 4 in this case? If so, why does str[4] give a out of range error?
@astel23: Please reread my answer. There is nothing "at" position 4. Find only cares about slices.
Thanks for the edit. Even though your bolded part isn't inside your Edit block, I believe you have changed it. Your former answer said The last position is after the last character of the string, that was cause of my confusion. Can you explain to me what you mean by find returns slice starts. You are supposed to slice the string starting at the given position.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.