Posts: 212
Threads: 25
Joined: Aug 2019
Oct-22-2019, 11:31 AM
(This post was last modified: Oct-22-2019, 11:34 AM by newbieAuggie2019.)
Hi!
I was just reading that the ' in' and ' not in' operators can be used with strings just like with list values and that an expression with two strings joined using ' in' or ' not in' will evaluate to a Boolean True or False.
That's fine with me, but I cannot understand the logic of the first case of these inputs and outputs:
Output: >>> '' in 'spam'
True
>>> '' in ' spam'
True
>>> '' in 'spam '
True
>>> '' in ' spam '
True
>>>
I can understand why "" is True in the last 3 cases ...
Wait! I think my light bulb has just turned on ... I thought it meant this:
Output: >>> ' ' in 'spam'
False
>>> ' ' in ' spam'
True
>>> ' ' in 'spam '
True
>>> ' ' in ' spam '
True
>>>
but probably the concept of "" is similar to ' the empty set' in set theory, so:
For any set A:
The empty set is a subset of A:
∀ A : ∅ ⊆ A https://en.m.wikipedia.org/wiki/Empty_set
So dealing with strings, an empty string (represented by "" or '') is included in any string, like an empty set is included in any set.
Is that it?
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 4,220
Threads: 97
Joined: Sep 2016
I don't know if that was Guido's reasoning, but that is certainly the behavior. The empty string is in every string, to the point of being in the empty string.
Posts: 212
Threads: 25
Joined: Aug 2019
(Oct-22-2019, 02:22 PM)ichabod801 Wrote: The empty string is in every string, to the point of being in the empty string.
Thank you!
One doubt less!!!
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 1,950
Threads: 8
Joined: Jun 2018
This behaviour pointed out in documentation: Membership test operations
Quote:Empty strings are always considered to be a substring of any other string, so "" in "abc" will return True.
One way of mentally parsing it will be remember that slicing string will return empty string even if index is out of range (and not None or whatever) and you can verify that there is empty string in every string and every character in string is considered empty string as well:
>>> a = ''
>>> a[0:0]
''
>>> a[0:]
''
>>> a[1:]
''
>>> a[7:42]
''
>>> ''.count('')
1
>>> 'a'.count('')
2
>>> 'ab'.count('')
3
>>> 'abc'.count('')
4
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 212
Threads: 25
Joined: Aug 2019
(Oct-23-2019, 09:11 AM)perfringo Wrote: Quote:Empty strings are always considered to be a substring of any other string [ ... ]. Thank you for your answer!
I can now understand that, comparing it to 'set theory'.
(Oct-23-2019, 09:11 AM)perfringo Wrote: [ ... ] there is empty string in every string [ ... ] I can now understand that, for the same reason as before.
(Oct-23-2019, 09:11 AM)perfringo Wrote: [ ... ] and every character in string is considered empty string as well Wow! That's a bit more difficult for me to comprehend. I can understand that 'every character is a string' and that from the axiom that 'there is empty string in every string' or expressed in other terms, that 'Empty strings are always considered to be a substring of any other string', I can see as a corollary that 'therefore any string of any character HAS an empty string in it', but I cannot manage to see 'any string of any character BEING an empty string'. I'm not sure if I have clearly explained myself.
All the best,
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 4,220
Threads: 97
Joined: Sep 2016
I think this may help:
Python Documentation Wrote:For the string and bytes types, x in y is True if and only if x is a substring of y. An equivalent test is y.find(x) != -1. Empty strings are always considered to be a substring of any other string, so "" in "abc" will return True.
The thing is, y.find('') is always 0. So every string sort of starts with an empty string. And so does every substring, as 'abc'.find('', 1) is 1 and 'abc'.find('', 2) is 2 and 'abc'.find('', 3) is 3.
Posts: 212
Threads: 25
Joined: Aug 2019
(Oct-23-2019, 02:14 PM)ichabod801 Wrote: I think this may help:
Python Documentation Wrote:For the string and bytes types, x in y is True if and only if x is a substring of y. An equivalent test is y.find(x) != -1. Empty strings are always considered to be a substring of any other string, so "" in "abc" will return True.
Thank you!
newbieAuggie2019
"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Posts: 1,950
Threads: 8
Joined: Jun 2018
I agree that ichabod801 explanation is much better.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
|