How to check if the string is empty?

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

    I would test noneness before stripping. Also, I would use the fact that empty strings are False (or Falsy). This approach is similar to Apache's StringUtils.isBlank or Guava's Strings.isNullOrEmpty

    This is what I would use to test if a string is either None OR Empty OR Blank:

    def isBlank (myString):
        if myString and myString.strip():
            #myString is not None AND myString is not empty or blank
            return False
        #myString is None OR myString is empty or blank
        return True
    

    And, the exact opposite to test if a string is not None NOR Empty NOR Blank:

    def isNotBlank (myString):
        if myString and myString.strip():
            #myString is not None AND myString is not empty or blank
            return True
        #myString is None OR myString is empty or blank
        return False
    

    More concise forms of the above code:

    def isBlank (myString):
        return not (myString and myString.strip())
    
    def isNotBlank (myString):
        return bool(myString and myString.strip())
    
    0 讨论(0)
  • 2020-11-22 15:21

    for those who expect a behaviour like the apache StringUtils.isBlank or Guava Strings.isNullOrEmpty :

    if mystring and mystring.strip():
        print "not blank string"
    else:
        print "blank string"
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 15:24

    The only really solid way of doing this is the following:

    if "".__eq__(myString):
    

    All other solutions have possible problems and edge cases where the check can fail.

    len(myString)==0 can fail if myString is an object of a class that inherits from str and overrides the __len__() method.

    Similarly myString == "" and myString.__eq__("") can fail if myString overrides __eq__() and __ne__().

    For some reason "" == myString also gets fooled if myString overrides __eq__().

    myString is "" and "" is myString are equivalent. They will both fail if myString is not actually a string but a subclass of string (both will return False). Also, since they are identity checks, the only reason why they work is because Python uses String Pooling (also called String Internment) which uses the same instance of a string if it is interned (see here: Why does comparing strings using either '==' or 'is' sometimes produce a different result?). And "" is interned from the start in CPython

    The big problem with the identity check is that String Internment is (as far as I could find) that it is not standardised which strings are interned. That means, theoretically "" is not necessary interned and that is implementation dependant.

    The only way of doing this that really cannot be fooled is the one mentioned in the beginning: "".__eq__(myString). Since this explicitly calls the __eq__() method of the empty string it cannot be fooled by overriding any methods in myString and solidly works with subclasses of str.

    Also relying on the falsyness of a string might not work if the object overrides it's __bool__() method.

    This is not only theoretical work but might actually be relevant in real usage since I have seen frameworks and libraries subclassing str before and using myString is "" might return a wrong output there.

    Also, comparing strings using is in general is a pretty evil trap since it will work correctly sometimes, but not at other times, since string pooling follows pretty strange rules.

    That said, in most cases all of the mentioned solutions will work correctly. This is post is mostly academic work.

    0 讨论(0)
  • 2020-11-22 15:26

    In case this is useful to someone, here is a quick function i built out to replace blank strings with N/A's in lists of lists (python 2).

    y = [["1","2",""],["1","4",""]]
    
    def replace_blank_strings_in_lists_of_lists(list_of_lists):
        new_list = []
        for one_list in list_of_lists:
            new_one_list = []
            for element in one_list:
                if element:
                    new_one_list.append(element)
                else:
                    new_one_list.append("N/A")
            new_list.append(new_one_list)
        return new_list
    
    
    x= replace_blank_strings_in_lists_of_lists(y)
    print x
    

    This is useful for posting lists of lists to a mysql database that does not accept blanks for certain fields (fields marked as NN in schema. in my case, this was due to a composite primary key).

    0 讨论(0)
提交回复
热议问题