1

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:

string.find("")

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

closed as off-topic by user22815, gnat, candied_orange, amon, svick Sep 8 '16 at 11:36

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for assistance in writing or debugging existing code are off-topic on Programmers. These questions can be asked on Stack Overflow if they include the desired behavior, a specific problem or error, and the shortest code necessary to reproduce it in the question itself. See How To Create a Minimal, Complete, and Verifiable Example." – Community, gnat, candied_orange
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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
7

"" 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-"
  • 7
    You asked me to find nothing. I found some nothing between these somethings. :) – candied_orange Sep 8 '16 at 4:59
9

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.

  • 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
2

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

1

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.

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