How to check if a string is empty in python?

  • Thread starter noagbodjivictor
  • Start date
R

Roy Smith

Ant said:
In Steven's defence, string exceptions *are* probably worth less, as
there's no longer such a demand for them.

You just wait until they start showing up on Antiques Roadshow :)
 
M

mensanator

...



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

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>
gmpy.mpz('zzz',26)
ValueError: invalid digits

until you remember that base 26 is 0-9a-p, not a-z.
 
M

mensanator

Seems like a fair assumption given the OP's question and example.

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.
 
J

Jaswant

This is a simple way to do it i think


s=hello
.... print "Empty"
.... else:
.... print s
....
hello
 
T

Tim Williams

This is a simple way to do it i think


s=hello

.... print "Empty"
.... else:
.... print s
....
hello

Not as simple as " If not s: "

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

 
M

mensanator

This is a simple way to do it i think

s=hello


... print "Empty"
... else:
... print s
...
hello

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.
2

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
len().

Dynamic typing is a feature, but that doesn't relieve you of the
necessary tests.
 
L

Larry Bates

Alex said:
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

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

-Larry
 
M

mensanator

Your point would be important if the question were "How can I tell if x
is an empty string?" On the other hand, "How to check if a string is
empty?" implies that the OP already knows it is a string. Maybe he's
been using string methods on it, maybe he got it from a function that he
knows provides a string. Maybe he's checked its type. It doesn't really
matter, if he's aware it is a string he doesn't have to test it for
stringness.

OTOH, some don't know enough to quote their string literals, so I
think
my point is well justified.
 
A

Alex Martelli

Larry Bates said:
Isn't deprecated like depreciated but not quite to zero yet?

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
 
M

mensanator

   ...


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".

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
can
no longer extract any tax benefits from it).
 
J

John Machin

...


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".

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top