r/learnpython • u/bkabartsy • Sep 26 '24
Empty strings - am i tripping?
>>> '' in 'abc'
Truecan someone explain? (python 3.12.6)
1
Upvotes
15
u/theWyzzerd Sep 26 '24 edited Sep 26 '24
Why would this not be true? Every string has an empty string in it. If "" is in "" why shouldn't it also be in "a"?
Think of it this way -- if a string is the set of characters "abc" what are you left with if you drop "a" and "b" and "c" from the string? You're left with "", which means it was in string "abc" all along.
4
u/This_Growth2898 Sep 26 '24
In the string 'abc', 'b' is between 'a' and 'c'.
In the string 'ac', '' is between 'a' and 'c'.
So, 'b' in 'abc' should be as true as '' in 'ac'.
53
u/Binary101010 Sep 26 '24
Directly from the Python docs:
https://docs.python.org/3/reference/expressions.html#membership-test-operations