How to check if the string is empty?

后端 未结 25 1847
佛祖请我去吃肉
佛祖请我去吃肉 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:11

    I did some experimentation with strings like '', ' ', '\n', etc. I want isNotWhitespace to be True if and only if the variable foo is a string with at least one non-whitespace character. I'm using Python 3.6. Here's what I ended up with:

    isWhitespace = str is type(foo) and not foo.strip()
    isNotWhitespace = str is type(foo) and not not foo.strip()
    

    Wrap this in a method definition if desired.

提交回复
热议问题