How to check if the string is empty?

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

    From PEP 8, in the “Programming Recommendations” section:

    For sequences, (strings, lists, tuples), use the fact that empty sequences are false.

    So you should use:

    if not some_string:
    

    or:

    if some_string:
    

    Just to clarify, sequences are evaluated to False or True in a Boolean context if they are empty or not. They are not equal to False or True.

提交回复
热议问题