Python Forum
A doubt with 'in' and 'not in' operators with strings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A doubt with 'in' and 'not in' operators with strings
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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
Reply
#4
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.
Reply
#5
(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
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
(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
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of if - and operators Pedro_Castillo 1 459 Oct-24-2023, 08:33 AM
Last Post: deanhystad
  Trying to understand strings and lists of strings Konstantin23 2 696 Aug-06-2023, 11:42 AM
Last Post: deanhystad
Question Doubt about conditionals in Python. Carmazum 6 1,536 Apr-01-2023, 12:01 AM
Last Post: Carmazum
  A simple python doubt mohamedrabeek 2 692 Mar-26-2023, 07:24 PM
Last Post: deanhystad
  Mixing Boolean and comparison operators Mark17 3 1,371 Jul-11-2022, 02:20 AM
Last Post: perfringo
  Splitting strings in list of strings jesse68 3 1,702 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  doubt about python tkinter and sqlite3 LONDER 2 2,118 Aug-14-2021, 08:48 AM
Last Post: ibreeden
  Python Doubt csrlima 5 2,535 Jan-23-2021, 12:23 AM
Last Post: csrlima
  Magic Method Arithmetic Operators ClownPrinceOfCrime 3 2,280 Jan-10-2021, 03:24 PM
Last Post: ndc85430
  Class and Operators in Python rsherry8 1 1,957 May-27-2020, 07:09 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020