r/learnpython • • Sep 26 '24

Empty strings - am i tripping?

>>> '' in 'abc'
True

can someone explain? (python 3.12.6)

1 Upvotes

27 comments sorted by

View all comments

53

u/Binary101010 Sep 26 '24

Directly from the Python docs:

Empty strings are always considered to be a substring of any other string, so "" in "abc" will return True.

https://docs.python.org/3/reference/expressions.html#membership-test-operations

0

u/[deleted] Sep 26 '24

[deleted]

-77

u/my_password_is______ Sep 26 '24

oh come on

that is NOT a good reason for a programming language

you can LITERALLY see '' is NOT in 'abc'

using standard set theory you end up with stupid shit like this

if '' in 'abc' and '' in 'def' :              
    print('this is stupid')

44

u/aplarsen Sep 26 '24

Found the angsty teen who doesn't like math

58

u/Jello_Penguin_2956 Sep 26 '24

I disagree. I can see that empty string before a, between a b c, and after c.

Its not a space

26

u/a2242364 Sep 26 '24

if you took intro to set-theory/discrete maths/theory of computation, you would know that this makes complete sense.

12

u/[deleted] Sep 26 '24

‘’ is in ‘abc’. Are you confusing it with ‘ ‘?

10

u/wildpantz Sep 26 '24

Actually, one could argue there's infinite "" in "abc", just how 3 can be expressed as 2+1 but also as 2+1+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0+0.

Just because something doesn't make sense to you, doesn't mean it doesn't make sense.

3

u/zefciu Sep 26 '24

I think it is the best explanation. If you concatenate strings, it would be silly if you no longer could find the concatenated strings in the results. So it makes sense that you can find '' in 'a' + '' + 'bc'.

→ More replies (1)

9

u/fivepython Sep 26 '24

I highly recommend looking up what the null set is, because it might be confusing at first but it’s a result of an axiom of math, aka, professionally agreed upon

5

u/skeleton_craft Sep 26 '24

I mean that working literally is a cornerstone of modern math, and number theory. I don't think you fully understand what it is exactly you're complaining about.

→ More replies (1)

3

u/rasputin1 Sep 26 '24

I like how the example is literally OP's code

0

u/bkabartsy Sep 26 '24

TIL, thank you.

what is the pythonic way of initializing an empty string and writing a while loop to populate that string with user input?

my_string = '' # wrong
while my_string not in 'abc':  
    my_string = input()  
    print(f'not printed: {my_string}')

5

u/socal_nerdtastic Sep 26 '24

The cool modern way is with a walrus:

while (my_string := input()) not in "abc":
    print(f'not printed: {my_string}')

1

u/bkabartsy Sep 26 '24

very cool. definitely more pythonic than my_string = ' ' or my_string = 'x'

1

u/assembly_wizard Sep 26 '24

The walrus operator won't always fit so nicely into your problem, so here's how you would write this using a do-while loop:

my_string = None
while True:
    my_string = input()
    if my_string in 'abc':
        break
    print(f'not printed: {my_string}')

(in this specific case the walrus is better, but this while true pattern is still useful sometimes)

2

u/Temporary_Pie2733 Sep 26 '24

No need to initialize my_string before the loop begins; the first in-loop assignment is guaranteed to occur.

2

u/FrangoST Sep 26 '24

I would do:

my_string = ''
while  my_string == '' or my_string not in 'abc':
    my_string = input()
    print(f'not printed: {my_string}')

2

u/JamzTyson Sep 26 '24

If you rearrange, you can dispense with the additional comparison:

my_string = input("Enter a substring to search: ")
while my_string not in haystack:
    print(f'not printed: {my_string}')
    my_string = input("Enter a substring to search: ")

or for Python >= 3.8

while (my_string := input("Enter a substring to search: ")) not in haystack:
    print(f'not printed: {my_string}')

One thing to be careful about with the second solution is that the walrus expression must be in parentheses:

(my_string := input("Enter a substring to search: "))

0

u/jso__ Sep 26 '24

Try it with a list:

In [1]: "" in ["a", "b", "c"]
Out[1]: False

4

u/barry_z Sep 26 '24

This may still not be what they want - 'ab' in 'abc' would evaluate to True while 'ab' in ['a', 'b', 'c'] would evaluate to False.

-5

u/LuciferianInk Sep 26 '24

Penny whispers, "What is the difference between ```"