How to check if the string is empty?

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

    Empty strings are "falsy" which means they are considered false in a Boolean context, so you can just do this:

    if not myString:
    

    This is the preferred way if you know that your variable is a string. If your variable could also be some other type then you should use myString == "". See the documentation on Truth Value Testing for other values that are false in Boolean contexts.

提交回复
热议问题