Re: I don't quite get this "string".find()
From: Jaime Wyant (programmer.py_at_gmail.com)
Date: 11/11/04
- Next message: Caleb Hattingh: "Re: Concise idiom to initialize dictionaries"
- Previous message: Josiah Carlson: "Re: Simple thread pools"
- Maybe in reply to: Jaime Wyant: "I don't quite get this "string".find()"
- Next in thread: Tim Peters: "Re: I don't quite get this "string".find()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 11 Nov 2004 14:04:56 -0600 To: python-list@python.org
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@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
>
- Next message: Caleb Hattingh: "Re: Concise idiom to initialize dictionaries"
- Previous message: Josiah Carlson: "Re: Simple thread pools"
- Maybe in reply to: Jaime Wyant: "I don't quite get this "string".find()"
- Next in thread: Tim Peters: "Re: I don't quite get this "string".find()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|