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

Tim Peters tim.peters at gmail.com
Thu Nov 11 21:12:51 CET 2004


[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>.



More information about the Python-list mailing list