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

From: Tim Peters (tim.peters_at_gmail.com)
Date: 11/11/04


Date: Thu, 11 Nov 2004 15:12:51 -0500
To: python-list@python.org


[Jaime Wyant]
> Ahh, I see it now. It seems strange to me, but your find helped make
> sense of it.

Indeed, the only way to have "test".find("") *not* return 0 would be
to make a special case out of searching for an empty string. If you
don't make a special case of it, 0 seems to be the necessary result.

> 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 :).

Consistency and strangeness often go together <0.7 wink>. I find this
particular one ("every string starts with an empty string") strangest
in this context:

>>> "a" in "abc"
True
>>> "c" in "abc"
True
>>> "d" in "abc"
False
>>> "" in "abc"
True

In practice, my solution is not to search for empty strings <wink>.



Relevant Pages

  • 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: I dont quite get this "string".find()
    ... But I guess an empty string isn't nothing, ... Ahh, it's still darned strange:). ... > def find(haystack, needle): ...
    (comp.lang.python)
  • 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)
  • Re: WoW teaches the wrong things
    ... can find a baby jesus one (google divine interventions if you don't ... Ahh the strange and twisted things following links at random will ...
    (comp.sys.ibm.pc.games.rpg)