I'm studying Python and hung up on a simple problem. Let's say we have:

string = "hello"

When we invoke the find method on the string to find an empty string like this:


Why does that equal to 0? I thought it would equal to -1 since an empty space could not be found.

  • Empty "space" or empty "string"? – user22815 Sep 8 '16 at 3:42
  • I can find empty space (empty strings) just about everywhere. – gnasher729 Sep 8 '16 at 7:49
  • You get the first occurrence of the empty string. In fact, there are 6 times an infinite number of occurrences of "" in "hello". – Martin Maat Sep 8 '16 at 8:38

"" is an empty string (I wouldn't call it an empty space - but I get your meaning).

An empty string can be found everywhere that isn't a string. That is, a string that doesn't contain anything. This is probably true of many languages.

This means there is an empty string before h, after h, before e, and so on.

Think about it this way, we know that

"" == ""

is true

Empty string equals empty string. A return of -1 would mean a failure; i.e. a non-match.

Note that the "" isn't just at the beginning as some of the answers here would suggest. I.e. The string isn't just "" + "hello".

Have a look at this:

string = "hello"
string.replace("", "-")

>>> "-h-e-l-l-o-"
| improve this answer | |
  • 7
    You asked me to find nothing. I found some nothing between these somethings. :) – candied_orange Sep 8 '16 at 4:59

When a string x is of the form a + b you would expect x.find(a) to return 0 without exceptions.

Since the string "hello" is of the form "" + "hello", the result "hello".find("") == 0 is expected.

| improve this answer | |
  • This answer provides some great rationale behind the madness! – svidgen Sep 8 '16 at 18:18
  • It's not just a rationale. When you have to decide what "hello".find("") should be, you write down properties that the find function should have. And then you define the result according to these properties. Not here, but in some cases there is trouble because the properties you write down might contradict each other. – gnasher729 Sep 8 '16 at 23:50

"" is not a character. A character is exactly one of the 256 (or 128, or 65536 or whatever) atoms of text that you have available. A character (e.g. the space character) may look empty, but it's still exactly one of those atoms.

Strings are different. They consist of any number of characters, and this number can be 0. "" is the string with 0 characters - the empty string. It is no better or worse than other strings, and in fact is often extremely useful. Many algorithms on strings would have to be much more complicated if you didn't assume that the empty string exists, has length 0, and can be found wherever you want.

| improve this answer | |

When you use string.find(somesubstring) and it returns an index i, not equal to -1, that means that the substring of string starting at index i and having the same length as somesubstring is equal to somesubstring (and it's the smallest of such values).

Since "hello"[0:0], the substring starting at index 0 with length 0, is indeed equal to "", the returned value is correct.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.