Reputation: 9492
I know it's a basic question but please bear with me. Let's say if we have 4 strings below:
a = ''
b = 'apple'
c = 'orange'
d = 'banana'
So, normally if I want to check if any of the three string a
b
c
is empty, I could use len()
function.
if len(a) == 0 or len(b) == 0 or len(c) == 0:
return True
But then I thought it is too troublesome to write like above if I have many strings. So, I used
if not a:
return True
But, when i am checking for multiple strings b
c
d
using the above method, it returns True
and I am puzzled as non of the strings b
c
d
where empty.
if not b or c or d:
return True
What is going on?
Upvotes: 6
Views: 9593
Reputation: 7906
The problem lies with this line:
if not b or c or d:
You need to include the "not" condition for each string. So:
if not b or not c or not d:
You could also do it like this:
return '' in [a, b, c, d]
Upvotes: 9
Reputation: 6675
The not operator has higher precedence than or.
return not b or not c or not d
should work.
Upvotes: 3