suprita shankar
suprita shankar

Reputation: 1579

Understanding string "null" in python

I have 2 variables: x="null" and y=3

Following which I execute the command below:

if(x and y):
  print 'True'

Output:True

I am looking for guidance to understand this behavior better.

The answer to this question fixed my issue Most elegant way to check if the string is empty in Python?

But I want to understand the behavior of this. I want to know how an AND of "null" and a numeric value of 3 returns in 3 which in turn results in truthify value.

Upvotes: 1

Views: 16426

Answers (2)

galinette
galinette

Reputation: 9292

A non empty string is always True in python. So "null" evaluates to True in a boolean operation, just as "banana"

Upvotes: 4

Netwave
Netwave

Reputation: 42716

"" is an empty string, "null" is not:

In[2]: bool("")
Out[2]: False
In[3]: bool("null")
Out[3]: True

Playing with the console to test your code behaviour is a good practice.

Upvotes: 2

Related Questions