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

J

Jaime Wyant

Will someone explain this to me?
0

Why is the empty string found at position 0?

Thanks!
jw
 
G

Grant Edwards

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.

How so? There aren't an infinite number of places _in_ a
finite-length string.
 
S

Steven Bethard

Caleb Hattingh said:
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:
-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

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

David Fraser

Jaime said:
Will someone explain this to me?



0

Why is the empty string found at position 0?

It's found there because it is there!

At any position, you can find an empty string because it is so short...

David
 
G

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

Ah, but the question wasn't how many empty strings would fit,
but at how many places you could "find" one. :)

If you have three pins, then there are only three pinheads on
which you can find a dancing angel -- regardless of how many
angles can dance on the head of a pin (assuming it's not zero).
 
I

Isaac To

Jaime> Will someone explain this to me? Jaime> 0

Jaime> Why is the empty string found at position 0?

It is completely natural. "test".find("es") = 1 because 1 is the
smallest x such that "test"[x:x+len("es")] == "es" (i.e., the smallest
x such that the substring of "test" from position x starts with "es").
Now what is the smallest x such that "test"[x:x+len("")] == ""?

Regards,
Isaac.
 
S

Steve Holden

Jaime said:
Will someone explain this to me?



0

Why is the empty string found at position 0?

Think of it this way: "test".find("") returns the index of the first
position in "test" where a substring of length len("") occurs equal to "".

the-empty-set-is-a-subset-of-all-sets-ly y'rs - steve
 
C

Caleb Hattingh

Hi Jaime

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. It is a difficult situation, like dividing zero into a nonzero
number. (Are you asking why it specifically returns 0 and not INF or an
exception...?)

If you want to match a *blank*, you gotta do

but I expect you are aware of this and this was not your question. Also,
if you want to check whether a string is empty, I do

One of the gurus can probably answer you better than I.

thx
Caleb
 
C

Caleb Hattingh

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top