如何检查我的字符串是否在python中不为空

时间:2019-03-14 10:50:20

标签: python python-3.x

if Region != " " & State != " " & Sku != " ":
  

TypeError:&不支持的操作数类型:“ str”和“ str”

对于单个字符串我知道了,但是对于多个字符串则显示了以上错误

预先感谢

3 个答案:

答案 0 :(得分:3)

正如DeepSpace指出的那样,您的字符串不是空的。考虑:

if Region.strip() != "" and State.strip() != "" and Sku.strip() != "":

此外,您可能希望使用小写的变量名,因为这是一种很好的做法。

答案 1 :(得分:2)

使用and代替&

  if Region != " " and State != " " and Sku != " ":

答案 2 :(得分:-1)

在python中,您可以将字符串评估为布尔值。如果不为空,则返回True;如果为空,则返回True。因此,您必须对此进行编码:

if not Region and not State and not Sku: