How do I check for empty string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lphang
    New Member
    • Mar 2007
    • 1

    How do I check for empty string?

    I am reading a simple text file that will contains three set of string values
    as follow:
    "1234567", "ABC123456" , ""

    I have my codes as follow trying to check for an empty string, with the input string as above, I can never get it to display "testid is empty", Any idea why the test failed?:
    Code:
    FileData = open(filename, "r")
    Filecontents = FileData.readline().strip()
    FileData.close()
    
    testid = FileContents.split(",")[2]
    if (testid == ""):
       print "testid is empty"
    else:
       print "testid has value"
    Last edited by bartonc; Mar 14 '07, 11:18 PM. Reason: added [code][/code] tags
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by lphang
    I am reading a simple text file that will contains three set of string values
    as follow:
    "1234567", "ABC123456" , ""

    I have my codes as follow trying to check for an empty string, with the input string as above, I can never get it to display "testid is empty", Any idea why the test failed?:

    FileData = open(filename, "r")
    Filecontents = FileData.readli ne().strip()
    FileData.close( )

    testid = FileContents.sp lit(",")[2]
    if (testid == ""):
    print "testid is empty"
    else:
    print "testid has value"
    Im not exactly sure but first u should put it in code brackes when u post.
    Why are you stripping it twice? Maybe im wrong but i think u should strip it once.
    Other than that i dont know, maybe someone with more experience can answer this.

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by ilikepython
      Im not exactly sure but first u should put it in code brackes when u post.
      You are right. You can use the # button in Enhanced Mode. Thanks for the input.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by lphang
        I am reading a simple text file that will contains three set of string values
        as follow:
        "1234567", "ABC123456" , ""

        I have my codes as follow trying to check for an empty string, with the input string as above, I can never get it to display "testid is empty", Any idea why the test failed?:
        Code:
        FileData = open(filename, "r")
        Filecontents = FileData.readline().strip()
        FileData.close()
        
        testid = FileContents.split(",")[2]
        if (testid == ""):
           print "testid is empty"
        else:
           print "testid has value"
        That is a very good question since:

        >>> testStr = "hello, world,"
        >>> testList = testStr.split(" ,")
        >>> testList
        ['hello', ' world', '']
        >>> testList[2] == ""
        True
        >>>

        The best way to track these things down is to make lots of asignments as I have done here and print them as you go.
        Look for an empty

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          Originally posted by lphang
          I am reading a simple text file that will contains three set of string values
          as follow:
          "1234567", "ABC123456" , ""

          I have my codes as follow trying to check for an empty string, with the input string as above, I can never get it to display "testid is empty", Any idea why the test failed?:
          Code:
          FileData = open(filename, "r")
          Filecontents = FileData.readline().strip()
          FileData.close()
          
          testid = FileContents.split(",")[2]
          if (testid == ""):
             print "testid is empty"
          else:
             print "testid has value"

          its best to show a sample of what the file contents look like.
          Is it like this?:
          Code:
          "1234567", "ABC123456", ""
          if it is , the when you read the file and split them up, the double quotes "" will be of string type and its length will be 2. An empty string should be of length 0. you could also check for len(testid) == 0, for empty string.
          Also, you can put some print statements so you could debug your code
          Code:
          ....
          testid = FileContents.split(",")[2]
          print "testid value " , testid , type(testid), "length" , len(testid)
          ....

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            I did this in interactive as an example of reading the literal string below from a file:
            Code:
            "1234567", "ABC123456", ""
            Code:
            >>> s = raw_input()
            >>> s
            '"1234567", "ABC123456", ""'
            >>> sList = s.replace('"', '').split(', ')
            >>> sList
            ['1234567', 'ABC123456', '']
            >>> sList[2]
            ''
            >>> len(sList[2])
            0
            >>>
            Good advice ghostdog.
            Last edited by bvdet; Mar 15 '07, 03:12 AM. Reason: add comment

            Comment

            • bladez
              New Member
              • Oct 2008
              • 1

              #7
              the easiest way to check if its empty is simple :

              Code:
              _string = ""
              
              if len(_string) <= 0:
                print "Empty String"
              else:
                print "Not Empty String"
              
              Output:
                Empty String

              Comment

              • Smygis
                New Member
                • Jun 2007
                • 126

                #8
                The even MOAR simplelestest way of it is to simply do

                Code:
                string = ""
                
                if string:
                    do stuff
                stuff will not be done because an empty string is False

                Code:
                >>> s = ""
                >>> if s:
                	print "a"
                
                	
                >>>
                >>> bool("")
                False
                >>> bool("a")
                True
                >>>
                btw, whoa, necroposting, like a boss.
                Last edited by Smygis; Feb 8 '12, 12:50 PM. Reason: edit of the edit to edit the editing

                Comment

                Working...