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

From: Jaime Wyant (programmer.py_at_gmail.com)
Date: 11/11/04


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
>



Relevant Pages

  • Re: I dont quite get this "string".find()
    ... to make a special case out of searching for an empty string. ... Ahh, it's still darned strange:). ... my solution is not to search for empty strings <wink>. ...
    (comp.lang.python)
  • Re: Strange Overflow Error!
    ... Strange indeed...it sounds to me like you've covered all the obvious bases. ... Exploring a bit further the NULL problems, one bit of defensive coding you ... could try would be to concatenate it with an empty string on the left hand ...
    (microsoft.public.vb.database.ado)
  • Re: coredump in df_readascii
    ... OS: AS3 ... Reason: df_commentschars is null. ... Strange. ... You can set it to an empty string, ...
    (comp.graphics.apps.gnuplot)
  • Re: editing docvariable fields
    ... Very strange indeed, especially as vbcr *is* chr. ... the ASC() function. ... As for the empty string, I checked it -- perhaps it's ...
    (microsoft.public.word.vba.userforms)