How to check if the string is empty?

后端 未结 25 1837
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  -上瘾入骨i
    2020-11-22 15:22

    When you are reading file by lines and want to determine, which line is empty, make sure you will use .strip(), because there is new line character in "empty" line:

    lines = open("my_file.log", "r").readlines()
    
    for line in lines:
        if not line.strip():
            continue
    
        # your code for non-empty lines
    

提交回复
热议问题