4

"ABCDE" has no empty character. But when I type

"" in "ABCDE"

Python interpreter returns True.

Why?

is there any empty character in "ABCDE"? I don't think so.

And I also found when I use these code:

target = ''
src = 'ABCDE'
src.find(target)

it returns 0 instead of -1

Why is this?

12
  • 3
    What do you mean by "an empty character"? How many characters do you think the string "" contains? May 2, 2017 at 2:33
  • 2
    '' is even in ''… What are you actually trying to test? Whether your string contains a space? Or is longer than 0 characters?
    – deceze
    May 2, 2017 at 2:38
  • 8
    because 'ABCDE' can be represented as '' + 'ABCDE', than first occurence of empty string is zero position May 2, 2017 at 2:43
  • 3
    @BingSun, an "empty string" makes sense. An "empty character" is meaningless. In fact the empty string is a substring (in fact, is many substrings) of any string. Try 'foo'[0:0], 'foo'[1:0], etc. The empty string can in fact be found in the string 'ABCDE'. The documentation is quite clear about this as well. May 2, 2017 at 2:47
  • 2
    I would classify this as undefined operation. If you're trying to ask "can '' fit into the string x", then the answer is yes, since you can insert '' at any point in any string without changing it, so you can arguably find it in any string, so any string contains a ''. You could argue the exact opposite as well. There is no way to represent '' inside a string. A string can be '', a string cannot contain ''. Bottom line: '' in '...' simply makes no real sense and there's no particular correct answer I'd expect out of it.
    – deceze
    May 2, 2017 at 2:49

1 Answer 1

8

For people with background in languages where string objects represented as arrays of characters it may be surprising, but if we try to follow such approach like

string = 'ABCDE'
characters_list = list(string)

then

'' in characters_list

will be False statement.

Empty string probably came from mathematics, where it is a neutral element for binary operation of string concatenation, i. e. for every string a

a + empty_string == empty_string + a == a

where + is a string concatenation symbol. Then "substringing" can be defined as follows:

  • for every strings a, b we say a is substring of b iff exists strings c, d such that

    b == c + a + d
    

Let's denote a is substring of b as a in b.

With these definitions of empty string and substringing relation can be proved lemma

  • empty_string is a substring of any string a:

    a == (definition of empty_string) == empty_string + a == 
    == (definition of empty_string) == empty_string + empty_string + a
    

    then if we define c = empty_string and d = a:

    a == c + empty_string + d
    

    and by definition empty_string in a.

1
  • 2
    Awesome! I loved it that you didn't just end with the differences in representation (array of characters). Instead you did a discrete math proof. Thanks and I think we should learn to answer in this manner more often.
    – Ernest Han
    Apr 2, 2020 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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