如何将两个变量与python中的一个字符串进行比较? [英] How do I compare two variables against one string in python?

查看:26
本文介绍了如何将两个变量与python中的一个字符串进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 a 或 b 为空,我想打印一条消息.

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

这是我的尝试

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.

如何仅在 a 或 b 为空字符串时才执行打印语句?

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

推荐答案

更明确的解决方案是:

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')

这篇关于如何将两个变量与python中的一个字符串进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆