How to check if the string is empty?

后端 未结 25 1930
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 14:47

Does Python have something like an empty string variable where you can do:

if myString == string.empty:

Regardless, what\'s the most elegan

25条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 15:04

    If you just use

    not var1 
    

    it is not possible to difference a variable which is boolean False from an empty string '':

    var1 = ''
    not var1
    > True
    
    var1 = False
    not var1
    > True
    

    However, if you add a simple condition to your script, the difference is made:

    var1  = False
    not var1 and var1 != ''
    > True
    
    var1 = ''
    not var1 and var1 != ''
    > False
    

提交回复
热议问题