How to check if a string is empty in python?

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

  1. noagbodjivictor

    Roy Smith Guest

    You just wait until they start showing up on Antiques Roadshow :)
    Roy Smith, May 3, 2007
    1. Advertisements

  2. noagbodjivictor

    mensanator Guest

    Traceback (most recent call last):
    File "<pyshell#4>", line 1, in <module>
    gmpy.mpz("Hello, I am a string and definitely not empy!", 10)
    ValueError: invalid digits

    But you don't get a TypeError, you get a ValueError. And you might
    scratch your head over

    Traceback (most recent call last):
    File "<pyshell#5>", line 1, in <module>
    ValueError: invalid digits

    until you remember that base 26 is 0-9a-p, not a-z.
    mensanator, May 3, 2007
    1. Advertisements

  3. noagbodjivictor

    John Salerno Guest

    Seems like a fair assumption given the OP's question and example.
    John Salerno, May 3, 2007
  4. noagbodjivictor

    mensanator Guest

    A fair assumption for languages other than Python.

    Just because s was a string at some point in the past
    doesn't mean it's a string now.
    mensanator, May 3, 2007
  5. noagbodjivictor

    Jaswant Guest

    This is a simple way to do it i think

    .... print "Empty"
    .... else:
    .... print s
    Jaswant, May 4, 2007
  6. noagbodjivictor

    Tim Williams Guest

    Not as simple as " If not s: "

    and nowhere near as simple as " print s or 'Empty' " :) :)

    Tim Williams, May 4, 2007
  7. noagbodjivictor

    mensanator Guest

    But you are still making the assumption that s is a string.
    (BTW, you need quotes around your example.)

    For example:
    11 11

    Can you tell which one is the string? I.e., which one had quotes
    around it?

    If you correctly assume that it was b, then yes, your example works.

    If you incorrectly assume it was a, then the example doesn't work.
    Traceback (most recent call last):
    File "<pyshell#8>", line 1, in <module>
    print len(a)
    TypeError: object of type 'int' has no len()

    You have to know that a variable is a string before you try to do a

    Dynamic typing is a feature, but that doesn't relieve you of the
    necessary tests.
    mensanator, May 4, 2007
  8. noagbodjivictor

    Larry Bates Guest

    Isn't deprecated like depreciated but not quite to zero yet?

    Larry Bates, May 4, 2007
  9. noagbodjivictor

    mensanator Guest

    OTOH, some don't know enough to quote their string literals, so I
    my point is well justified.
    mensanator, May 5, 2007
  10. No. "To deprecate" comes from a Latin verb meaning "to ward off a
    disaster by prayer"; when you're saying you deprecate something, you're
    saying you're praying for that something to disappear, go away; in a
    secular context, you're earnestly imploring people to NOT do it.

    "To depreciate" comes from a Latin verb meaning "to reduce the price";
    when you're saying you depreciate something, you're saying you put on
    that something a lower price (and, by extension, a lower value) than it
    has (or, more commonly, used to have). You're not necessarily saying
    it's worth nothing at all (accountants sometimes deem an asset "fully
    depreciated" to mean something close to that, but the adverb "fully" is
    crucial to this meaning), just that it's worth "less than before".

    The two terms got somewhat entwined, no doubt because their spelling is
    so similar (even though etimology and pronunciation are poles apart),
    but the "correct" meanings and usage are still well distinct.

    Alex Martelli, May 5, 2007
  11. noagbodjivictor

    mensanator Guest

    But doesn'y "partially depreciated" also mean "less than before"?
    I thought "fully depreciated" meant the value AS AN ASSET was now 0,
    not the actual value, such as when my company replaces my perfectly
    functioning computer because it is "fully depreciated" (the company
    no longer extract any tax benefits from it).
    mensanator, May 5, 2007
  12. noagbodjivictor

    John Machin Guest

    Seeing this thread has already deteriorated [another word with Latin
    ancestry, not to be conflated with "posteriorated"] to the level of a
    debate about how many angels can stand shoulder-to-shoulder between
    the quotes surrounding the repr of an empty string, I presume it's OK
    if I remark that a "fully depreciated" asset is one that has a *book*
    value of zero [essentially that the whole of its original cost has
    been written off (i.e. claimed as a tax deduction)] , and that this
    has absolutely nothing to do with the true worth of the asset.
    John Machin, May 5, 2007
    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.