I don't quite get this "string".find()
Caleb Hattingh
caleb1 at telkomsa.net
Fri Nov 12 04:55:57 CET 2004
Steven and Grant (Edwards)
Err...discrete math was never my strong point :) Still, matching
'emptiness' to non-emptiness is what conceptually raises the problem for
me. I am not convinced that because there are only 'so many places' to
fit, one is restricted to a finite number of possibilities - simply
because what we are trying fit is 'nothing'.
Of course, I make this claim on nothing more than a hunch and
indigestion. The field of discrete math must have resolved this kind of
thing at some point, and I'll accept that position. If that was in fact
the position you two just presented, I apologise.
thx
Caleb
On Thu, 11 Nov 2004 20:37:26 +0000 (UTC), Steven Bethard
<steven.bethard at gmail.com> wrote:
> Caleb Hattingh <caleb1 <at> telkomsa.net> writes:
>>
>> I don't know why the function was set up this way. However, an empty
>> string can be found in an infinite number of places within any other
>> string.
>
> Infinite might be an exaggeration. Since there are only a finite number
> of
> indices into a string (len(s) + 1), there are only a finite number of
> places an
> empty sting may be found in any given string:
>
>>>> s = 'abc'
>>>> s.find('')
> 0
>>>> s.find('', 1)
> 1
>>>> s.find('', 2)
> 2
>>>> s.find('', 3)
> 3
>>>> s.find('', 4)
> -1
>
> You can't, say, find the empty string somewhere between indices 1 and 2.
>
>
>> Also, if you want to check whether a string is empty, I do
>>
>> >>> "test" == ""
>> False
>
> An empty string evaluates to False in a boolean context, so you probably
> don't
> usually want to actually test like this. A couple of options:
>
>>>> s = ''
>>>> bool(s)
> False
>
>>>> s = ''
>>>> if s:
> ... print 'not empty'
> ... else:
> ... print 'empty'
> ...
> empty
>
> My suspicion is that any time you actually test against an empty string,
> you're
> probably doing this in the context of an if statement or a while loop,
> so you
> can simply use the string directly instead of testing anything.
>
> Steve
>
More information about the Python-list
mailing list