I don't quite get this "string".find()

Jaime Wyant programmer.py at gmail.com
Thu Nov 11 15:04:56 EST 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 :).

Thanks though!
jw

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] == ""
> True
> 
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list