Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare two variables against one string in python?

I would like to print a message if either a or b is empty.

This was my attempt

a = ""
b = "string"

if (a or b) == "":
    print "Either a or b is empty"

But only when both variables contain an empty string does the message print.

How do I execute the print statement only when either a or b is an empty string?

like image 830
Sheldon Avatar asked Dec 05 '22 15:12

Sheldon


2 Answers

The more explicit solution would be this:

if a == '' or b == '':
    print('Either a or b is empty')

In this case you could also check for containment within a tuple:

if '' in (a, b):
    print('Either a or b is empty')
like image 155
poke Avatar answered Mar 04 '23 07:03

poke


if not (a and b):
    print "Either a or b is empty"
like image 35
exfizik Avatar answered Mar 04 '23 07:03

exfizik