empty_string in some_string - always true?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping
--
Chapters
00:00 Empty_string In Some_string - Always True?
00:41 Answer 1 Score 6
01:11 Accepted Answer Score 6
02:46 Answer 3 Score 0
03:21 Thank you
--
Full question
https://stackoverflow.com/questions/1349...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python27 #string
#avk47
ANSWER 1
Score 6
Another way to look at it is that x in y should return True if it's possible to find two strings s1 and s2 such that the following holds:
s1 + x + s2 == y
When x is the empty string it will always give True. This is because you can choose s1 = '' and s2 = y.
Of course the actual implementation of in doesn't work in this way, but the result is the same. It just gets that result in a more efficient manner.
ACCEPTED ANSWER
Score 6
A string S is a substring of the string T if and only if there exist an index i such that T[i:i+len(S)] == S. When S is the 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's definitely the correct result.
In summary, the empty string is a substring of every string, and all those results are a direct consequence of this.
Also note that this is peculiar to strings, because strings are sequences of characters, which are themselves strings of length one. For other kind of sequences(such as lists or tuples) you do 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
That's because list and tuple only check for members, and not for sub-lists or sub-tuples, because their elements can be of arbitrary type.
Imagine the case ((1,2),1,2).index((1,2)). Should index check for "sub-tuples"(and thus return 1), for members(and thus return 0) or do some ugly mixture(e.g. first check for sub-tuples and then for members)? In python it was decided to search for members only, since it is simpler and it's usually what you want. Checking for sub-tuples only would give really odd results in the general case and doing "mixtures" would often yield unpredictable results.
ANSWER 3
Score 0
well, the answer is simple really. you are searching for "" which really, is not a character at all....
and the first time there is no character in a string, is right at the beginning... at [0]
it gets confusing, because you would think that [0] is the index of the FIRST character. this is true, but you must also think that in this case the segments between characters are also something "tangible" and so of course nothing is in something, its also in nothing. and if you were to check subsets of a string, you would find it anywhere you chose, in the first location possible.