Empty String Boolean Logic
Asked Answered
V

5

7

I just stumbled across this and I couldn't find a sufficient answer:

x = ""

Why then is:

x == True
False

x == False
False

x != True
True

x != False
True

Am I supposed to conclude that x is neither True nor False?

Viridis answered 15/3, 2016 at 13:57 Comment(5)
What?! You are to conclude that x isn't equal to either True or False. Why did you think it would be? Have you been confused somehow by docs.python.org/2/library/stdtypes.html#truth-value-testing? It will still evaluate false-y in a boolean context: if x:, bool(x), etc..Brogan
Dr. Tautology, I think what you meant to test was if bool(x) == False. Username checks out, though.Hairpin
Now see what happens with your tests if you set x=0. And then do the same thing with x=1.Warfield
@PM2Ring Ask one bad question on stackoverflow and it ruins your day.FMLViridis
Hey, it's not that bad. Sure, it's got a net zero score, but you won a few rep points from it, and you got some good answers. But yes, this is fairly basic info that isn't that hard to find in the official documentation.Warfield
C
6

to check if x is True of False:

bool("")
> False

bool("x")
> True

for details on the semantics of is and == see this question

Casket answered 15/3, 2016 at 14:1 Comment(0)
F
4

Am I supposed to conclude that x is neither True nor False?

That's right. x is neither True nor False, it is "". The differences start with the type:

>>> print(type(""), type("x"), type(True), type(False))
builtins.str, builtins.str, builtins.bool, builtins.bool

Python is a highly object oriented language. Hence, strings are objects. The nice thing with python is that they can have a boolean representation for if x: print("yes"), e. g.. For strings this representation is len(x)!=0.

Fid answered 15/3, 2016 at 14:18 Comment(0)
A
2

In python '==' tests for equality. The empty string is not equal to True, so the result of your comparison is False.

You can determine the 'truthiness' of the empty string by passing it to the bool function:

>>> x = ''
>>> bool(x)
False
Achromatin answered 15/3, 2016 at 14:1 Comment(0)
D
1

In a Boolean context, null / empty strings are false (Falsy). If you use

testString = ""

if not testString:
    print("NULL String")
else:
    print(testString)

As snakecharmerb said, if you pass the string to the bool() function it will return True or False based

>>> testString = ""
>>> bool(testString)
    False

>>> testString = "Not an empty string"
>>> bool(testString)
    True

See this doc on Truth Value Testing to learn more about this:

Python 2:

https://docs.python.org/2/library/stdtypes.html#truth-value-testing

Python 3:

https://docs.python.org/3/library/stdtypes.html#truth-value-testing

Doorbell answered 15/3, 2016 at 14:13 Comment(0)
H
0

x represents an empty string literal object. By itself it is not a boolean type value, but as the other answers suggest a "cast" to boolean is possible. When used in an if statement condition check expression, the expression will evaluate to "False-y" due to the string being empty.

However your comparison examples compare the object x to the singleton objects "True" and "False", which falls back to a "is" comparison. Since the objects are different, the compare evaluates to False for when checking equality.

(For details look at Returning NotImplemented from __eq__ )

Hauler answered 30/5 at 1:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.