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')

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.

1

u/Invader_Mars Sep 26 '24

Your comment made it clear as day. Cool idea on all this, TIL. Thanks