How to check if a string is empty in python?

Discussion in 'Python' started by noagbodjivictor, May 2, 2007.

  1. How to check if a string is empty in python?
    if(s == "") ??
     
    noagbodjivictor, May 2, 2007
    #1
    1. Advertisements

  2. noagbodjivictor

    Basilisk96 Guest

    A simple

    if s:
    print "not empty"
    else:
    print "empty"

    will do.


    -Basilisk96
     
    Basilisk96, May 2, 2007
    #2
    1. Advertisements



  3. Empty strings and containers are false; so
    one can write

    if (not s):
    print "something..."
     
    attn.steven.kuo, May 2, 2007
    #3
  4. noagbodjivictor

    mensanator Guest

    How do you know that s is a string?
     
    mensanator, May 2, 2007
    #4
  5. noagbodjivictor

    Basilisk96 Guest

    How do you know that s is a string?

    It's a presumption based on the original problem statement.

    The example shown is a simple T/F check, which happens to determine
    the "emptiness" of strings.
    If type checking is absolutely necessary, one could use

    if isinstance(s, basestring):
    if s:
    print "not empty"
    else:
    print "empty"
     
    Basilisk96, May 2, 2007
    #5
  6. Exactly so. "not s" works as well.

    Martin
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, May 2, 2007
    #6
  7. noagbodjivictor

    John Machin Guest

    Please lose the parentheses.
    if s == "": will work.
    So will if not s:

    Have you worked through the tutorial yet?
     
    John Machin, May 2, 2007
    #7
  8. In no particular order, all of these methods will work:


    # test s is equal to another empty string
    if s == "":

    # assuming s is a string, test that it is empty
    if not s:

    # test s is a string and it is empty
    if isinstance(s, str) and not s:

    # test s has length 0
    if len(s) == 0:

    # test the length of s evaluates as false
    if not len(s):

    # a long way to test the length of s
    if s.__len__() < 1:

    # a stupid way to test s is empty
    if bool(s) == False:

    # a REALLY stupid way to test s is empty
    if (bool(s) == False) == True:

    # test that appending s to itself is itself
    if s+s == s:

    # test that s has none of any character
    if not filter(None, [1 + s.find(chr(n)) for n in range(256)]):

    That last one is really only good for wasting CPU cycles.
     
    Steven D'Aprano, May 2, 2007
    #8
  9. noagbodjivictor

    John Machin Guest

    Have you considered
    if not re.match(".+$", s):
    ?
     
    John Machin, May 2, 2007
    #9
  10. a écrit :
    if not s:
    print "s is empty"
     
    Bruno Desthuilliers, May 3, 2007
    #10
  11. a écrit :
    Why do you want to know if it's a string ?
     
    Bruno Desthuilliers, May 3, 2007
    #11
  12. noagbodjivictor

    mensanator Guest

    Traceback (most recent call last):
    File "<pyshell#38>", line 1, in <module>
    gmpy.mpz(11,10)
    TypeError: gmpy.mpz() with numeric argument needs exactly 1 argument

    The mpz conversion takes two arguments if and only if s is a string,
    else it takes 1 argument. So being non-empty is insufficient.
     
    mensanator, May 3, 2007
    #12
  13. noagbodjivictor

    Dustan Guest

    and the other ones are... ?
     
    Dustan, May 3, 2007
    #13
  14. noagbodjivictor

    Roy Smith Guest

    s.join("foo") == "foo"

    for c in s:
    raise "it's not empty"
     
    Roy Smith, May 3, 2007
    #14
  15. Steven D'Aprano, May 3, 2007
    #15
  16. Being a string AND being non-empty is insufficient too -- just try

    gmpy.mpz("Hello, I am a string and definitely not empy!", 10)

    on for size.


    Alex
     
    Alex Martelli, May 3, 2007
    #16
  17. They're actually deprecated, not depreciated.

    Searching define:deprecated -- first hit:

    In computer software standards and documentation, deprecation is the
    gradual phasing-out of a software or programming language feature.
    en.wikipedia.org/wiki/Deprecated

    and the other four hits are fine too.

    Searching define:depreciated , we move almost entirely into accounting
    and finance, except:

    http://en.wikipedia.org/wiki/Depreciated
    """
    Depreciated is often confused or used as a stand-in for "deprecated";
    see deprecation for the use of depreciation in computer software
    """


    Alex
     
    Alex Martelli, May 3, 2007
    #17
  18. Er, um... oh yeah... I knew that...

    I mean... yes, that was deliberate, to see who was paying attention. Well
    done Alex!
     
    Steven D'Aprano, May 3, 2007
    #18
  19. noagbodjivictor

    Dustan Guest

    Being the slow person that I am, it really did take me this long to
    find a mistake.
    s+s != 'appending'
    s+s == 'concatenation'
     
    Dustan, May 3, 2007
    #19
  20. noagbodjivictor

    Ant Guest

    In Steven's defence, string exceptions *are* probably worth less, as
    there's no longer such a demand for them.
     
    Ant, May 3, 2007
    #20
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.