Re: How to check if a string is empty in python?

2007-05-04 Thread John Machin
On May 5, 12:19 pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
> Larry Bates <[EMAIL PROTECTED]> wrote:
>
>...
>
> > 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".

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.




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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-04 Thread [EMAIL PROTECTED]
On May 4, 9:19�pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
> Larry Bates <[EMAIL PROTECTED]> wrote:
>
> � �...
>
> > 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".

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

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


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to check if a string is empty in python?

2007-05-04 Thread Alex Martelli
Larry Bates <[EMAIL PROTECTED]> wrote:
   ...
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-04 Thread [EMAIL PROTECTED]
On May 4, 1:31 pm, "Hamilton, William " <[EMAIL PROTECTED]> wrote:
> > -Original Message-
> > From: [EMAIL PROTECTED]
>
> > On May 4, 5:02 am, Jaswant <[EMAIL PROTECTED]> wrote:
> > > This is  a simple way to do it i think
>
> > > s=hello
>
> > > >>> if(len(s)==0):
>
> > > ... 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:
>
> > >>> print a,b
> > 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.
>
> > >>> print len(b)
> > 2
>
> > If you incorrectly assume it was a, then the example doesn't work.
>
> > >>> print len(a)
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > 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.
>
> 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.

>
> ---
> -Bill Hamilton

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-04 Thread Larry Bates
Alex Martelli wrote:
> Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> 
>> On Wed, 02 May 2007 21:19:54 -0400, Roy Smith wrote:
>>
>>> for c in s:
>>>raise "it's not empty"
>> String exceptions are depreciated and shouldn't be used.
>>
>> http://docs.python.org/api/node16.html
> 
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: How to check if a string is empty in python?

2007-05-04 Thread Hamilton, William
> -Original Message-
> From: [EMAIL PROTECTED]
> 
> On May 4, 5:02 am, Jaswant <[EMAIL PROTECTED]> wrote:
> > This is  a simple way to do it i think
> >
> > s=hello
> >
> > >>> if(len(s)==0):
> >
> > ... 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:
> 
> >>> print a,b
> 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.
> 
> >>> print len(b)
> 2
> 
> If you incorrectly assume it was a, then the example doesn't work.
> 
> >>> print len(a)
> Traceback (most recent call last):
>   File "", line 1, in 
> 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.

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.


---
-Bill Hamilton
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-04 Thread [EMAIL PROTECTED]
On May 4, 5:02 am, Jaswant <[EMAIL PROTECTED]> wrote:
> This is  a simple way to do it i think
>
> s=hello
>
> >>> if(len(s)==0):
>
> ... 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:

>>> print a,b
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.

>>> print len(b)
2

If you incorrectly assume it was a, then the example doesn't work.

>>> print len(a)
Traceback (most recent call last):
  File "", line 1, in 
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-04 Thread Tim Williams
On 4 May 2007 03:02:37 -0700, Jaswant <[EMAIL PROTECTED]> wrote:
> This is  a simple way to do it i think
>
>
> s=hello
>
> >>> if(len(s)==0):
>  print "Empty"
>  else:
>  print s
> 
> hello

Not as simple as   " If not s: "

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


>>> s = ''
>>> print s or 'Empty'
Empty
>>> s = 'hello'
>>> print s or 'Empty'
hello
>>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-04 Thread Jaswant
This is  a simple way to do it i think


s=hello

>>> if(len(s)==0):
... print "Empty"
... else:
... print s
...
hello

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-03 Thread [EMAIL PROTECTED]
On May 3, 2:03 pm, John Salerno <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > On May 2, 3:49 pm, Basilisk96 <[EMAIL PROTECTED]> wrote:
> >> A simple
>
> >> if s:
> >> print "not empty"
> >> else:
> >> print "empty"
>
> >> will do.
>
> > How do you know that s is a string?
>
> 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-03 Thread John Salerno
[EMAIL PROTECTED] wrote:
> On May 2, 3:49 pm, Basilisk96 <[EMAIL PROTECTED]> wrote:
>> A simple
>>
>> if s:
>> print "not empty"
>> else:
>> print "empty"
>>
>> will do.
> 
> How do you know that s is a string?

Seems like a fair assumption given the OP's question and example.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-03 Thread [EMAIL PROTECTED]
On May 2, 11:59 pm, [EMAIL PROTECTED] (Alex Martelli) wrote:
> [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>...
>
> > >>> import gmpy
> > >>> gmpy.mpz(11)
> > mpz(11)
> > >>> gmpy.mpz('11',10)
> > mpz(11)
> > >>> gmpy.mpz(11,10)
>
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > 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.
>
> 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

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

Traceback (most recent call last):
  File "", line 1, in 
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

>>> gmpy.mpz('zzz',26)

Traceback (most recent call last):
  File "", line 1, in 
gmpy.mpz('zzz',26)
ValueError: invalid digits

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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-03 Thread Roy Smith
In article <[EMAIL PROTECTED]>,
 Ant <[EMAIL PROTECTED]> wrote:

> On May 3, 5:59 am, [EMAIL PROTECTED] (Alex Martelli) wrote:
> > Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> > > On Wed, 02 May 2007 21:19:54 -0400, Roy Smith wrote:
> >
> > > > for c in s:
> > > >raise "it's not empty"
> >
> > > String exceptions are depreciated and shouldn't be used.
> >
> > >http://docs.python.org/api/node16.html
> >
> > They're actually deprecated, not depreciated.
> 
> 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 :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-03 Thread Ant
On May 3, 5:59 am, [EMAIL PROTECTED] (Alex Martelli) wrote:
> Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> > On Wed, 02 May 2007 21:19:54 -0400, Roy Smith wrote:
>
> > > for c in s:
> > >raise "it's not empty"
>
> > String exceptions are depreciated and shouldn't be used.
>
> >http://docs.python.org/api/node16.html
>
> They're actually deprecated, not depreciated.

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

--
Ant...

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-03 Thread Dustan
On May 2, 5:50 pm, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Wed, 02 May 2007 13:35:47 -0700, noagbodjivictor wrote:
> > How to check if a string is empty in python?
> > if(s == "") ??
>
> 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:

Being the slow person that I am, it really did take me this long to
find a mistake.
s+s != 'appending'
s+s == 'concatenation'

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


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Steven D'Aprano
On Wed, 02 May 2007 21:59:56 -0700, Alex Martelli wrote:

> Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> 
>> On Wed, 02 May 2007 21:19:54 -0400, Roy Smith wrote:
>> 
>> > for c in s:
>> >raise "it's not empty"
>> 
>> String exceptions are depreciated and shouldn't be used.
>> 
>> http://docs.python.org/api/node16.html
> 
> They're actually deprecated, not depreciated.

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 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Alex Martelli
Steven D'Aprano <[EMAIL PROTECTED]> wrote:

> On Wed, 02 May 2007 21:19:54 -0400, Roy Smith wrote:
> 
> > for c in s:
> >raise "it's not empty"
> 
> String exceptions are depreciated and shouldn't be used.
> 
> http://docs.python.org/api/node16.html

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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
   ...
> >>> import gmpy
> >>> gmpy.mpz(11)
> mpz(11)
> >>> gmpy.mpz('11',10)
> mpz(11)
> >>> gmpy.mpz(11,10)
> 
> Traceback (most recent call last):
>   File "", line 1, in 
> 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.

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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Steven D'Aprano
On Wed, 02 May 2007 21:19:54 -0400, Roy Smith wrote:

> for c in s:
>raise "it's not empty"

String exceptions are depreciated and shouldn't be used.

http://docs.python.org/api/node16.html



-- 
Steven D'Aprano 


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Roy Smith
In article <[EMAIL PROTECTED]>,
 Dustan <[EMAIL PROTECTED]> wrote:

> On May 2, 5:50 pm, Steven D'Aprano
> <[EMAIL PROTECTED]> wrote:
> > On Wed, 02 May 2007 13:35:47 -0700, noagbodjivictor wrote:
> > > How to check if a string is empty in python?
> > > if(s == "") ??
> >
> > 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:
> 
> LOL
> 
> > # 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.
> 
> and the other ones are... ?
> 
> > --
> > Steven.

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

for c in s:
   raise "it's not empty"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Dustan
On May 2, 5:50 pm, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Wed, 02 May 2007 13:35:47 -0700, noagbodjivictor wrote:
> > How to check if a string is empty in python?
> > if(s == "") ??
>
> 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:

LOL

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

and the other ones are... ?

> --
> Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread [EMAIL PROTECTED]
On May 2, 6:41 pm, Bruno Desthuilliers
<[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] a écrit :
>
> > On May 2, 3:49 pm, Basilisk96 <[EMAIL PROTECTED]> wrote:
>
> >>A simple
>
> >>if s:
> >>print "not empty"
> >>else:
> >>print "empty"
>
> >>will do.
>
> > How do you know that s is a string?
>
> Why do you want to know if it's a string ?

>>> import gmpy
>>> gmpy.mpz(11)
mpz(11)
>>> gmpy.mpz('11',10)
mpz(11)
>>> gmpy.mpz(11,10)

Traceback (most recent call last):
  File "", line 1, in 
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> On May 2, 3:49 pm, Basilisk96 <[EMAIL PROTECTED]> wrote:
> 
>>A simple
>>
>>if s:
>>print "not empty"
>>else:
>>print "empty"
>>
>>will do.
> 
> 
> How do you know that s is a string?

Why do you want to know if it's a string ?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread John Machin
On May 3, 8:50 am, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Wed, 02 May 2007 13:35:47 -0700, noagbodjivictor wrote:
> > How to check if a string is empty in python?
> > if(s == "") ??
>
> In no particular order, all of these methods will work:
>
[snip]
>
> # 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.

Have you considered
if not re.match(".+$", s):
?


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> How to check if a string is empty in python?
> if(s == "") ??
> 

if not s:
   print "s is empty"


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Steven D'Aprano
On Wed, 02 May 2007 13:35:47 -0700, noagbodjivictor wrote:

> How to check if a string is empty in python?
> if(s == "") ??

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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread [EMAIL PROTECTED]
On May 2, 3:49 pm, Basilisk96 <[EMAIL PROTECTED]> wrote:
> A simple
>
> if s:
> print "not empty"
> else:
> print "empty"
>
> will do.

How do you know that s is a string?

>
> -Basilisk96


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread John Machin
On May 3, 6:35 am, [EMAIL PROTECTED] wrote:
> How to check if a string is empty in python?
> if(s == "") ??

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

Have you worked through the tutorial yet?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Martin v. Löwis
[EMAIL PROTECTED] schrieb:
> How to check if a string is empty in python?
> if(s == "") ??

Exactly so. "not s" works as well.

Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Basilisk96
> 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"


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread attn . steven . kuo
On May 2, 1:35 pm, [EMAIL PROTECTED] wrote:
> How to check if a string is empty in python?
> if(s == "") ??



Empty strings and containers are false; so
one can write

if (not s):
print "something..."


--
Hope this helps,
Steven

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to check if a string is empty in python?

2007-05-02 Thread Basilisk96
A simple

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

will do.


-Basilisk96

-- 
http://mail.python.org/mailman/listinfo/python-list