Question

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?

Was it helpful?

Solution

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]

OTHER TIPS

The not operator has higher precedence than or.

return not b or not c or not d

should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top