Empty_string in some_string - always right?

Possible duplicate:
Why is an empty line in each line?

I wonder why Python returns True whenever I check if there is an empty string in the string and why its index is zero.

For example:

  • '' in '' => true
  • ''.index('') => 0
  • '' in 'notEmpty' => true
  • 'notEmpty'.index('') => 0

I noticed this when I wrote the ROT13 function and tested it, I found that when I call it on an empty string, it returns 'n' ( 'n' is index13 in the alphabet).

+8
python string
source share
3 answers

String S is a substring of string T if and only if there exists an index i such that T[i:i+len(S)] == S When S is an empty string, you have T[i:i] = '' = S , so all the results are correct.

Also note that T.index('') returns 0, because index returns the first index in which the substring appears, and T[0:0] = '' so that the result is definitely correct.

Thus, an empty string is a substring of each string, and all these results are a direct consequence of this.

Also note that this is characteristic of strings, because strings are sequences of characters that themselves are strings of length one. For other types of sequences (e.g. list or tuple s) you will not get the same results:

 >>> (1,2,3).index(()) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: tuple.index(x): x not in tuple >>> [1,2,3].index([1,2]) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: [1, 2] is not in list >>> [] in [1,2,3] False 

This is because list and tuple only check members, not under list or sub << 210> s, because their elements can be of any type. Imagine the case of ((1,2),1,2).index((1,2)) . Should index check "substrings" (and therefore return 1) for members (and therefore return 0) or make some ugly mix (for example, check subcategories first and then for members)? In python, it was decided to search for members only, as this is simpler, and usually this is what you want. Checking for individual tuples would lead to really odd results in the general case, and performing "mixtures" would often lead to unpredictable results.

+6
source share

Another way to look at this is that x in y should return True if you can find two lines s1 and s2 such that the following is true:

 s1 + x + s2 == y 

When x is an empty string, it will always give True . This is because you can choose s1 = '' and s2 = y .

Of course, the actual in implementation does not work that way, but the result is the same. This leads to a more effective result.

+6
source share

well, the answer is actually simple. you are looking for "" , which really is not a character at all ....

and the first time there is no character in the line, it is at the beginning ... at [0]

it gets confused because you think that [0] is the index of the FIRST character. this is true, but you should also think that in this case the segments between the characters are also β€œtangible”, and therefore, of course, nothing is in something, it is also nothing. and if you were to check the subsets of the string, you will find it anywhere you choose in the first place.

0
source share

All Articles