I don't quite get this "string".find()
programmer.py at gmail.com
Thu Nov 11 21:04:56 CET 2004
Ahh, I see it now. It seems strange to me, but your find helped make
sense of it.
I guess I thought:
1) an empty string is like "nothing"
2) you can never find "nothing" in something
But I guess an empty string isn't nothing, but a string with no
length. Ahh, it's still darned strange :).
On Thu, 11 Nov 2004 14:57:12 -0500, Tim Peters <tim.peters at gmail.com> wrote:
> [Jaime Wyant]
> > Will someone explain this to me?
> > >>> "test".find("")
> > 0
> > Why is the empty string found at position 0?
> Because index 0 is the smallest index at which "" is found:
> >>> "test"[0:0] == ""
> As a string method, find() acts like this (skipping obfuscating optimizations):
> def find(haystack, needle):
> for i in range(len(haystack)):
> if haystack[i : i+len(needle)] == needle:
> return i
> return -1
More information about the Python-list