|
How to check if a string is empty in python?
if(s == "") ?? | |
Share:
|
A simple
if s:
print "not empty"
else:
print "empty"
will do.
-Basilisk96 | | |
On May 2, 1:35 pm, noagbodjivic...@gmail.com 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 | | |
On May 2, 3:49 pm, Basilisk96 <basilis...@gmail.comwrote:
A simple
if s:
print "not empty"
else:
print "empty"
will do.
How do you know that s is a string?
>
-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" | | | no*************@gmail.com schrieb:
How to check if a string is empty in python?
if(s == "") ??
Exactly so. "not s" works as well.
Martin | | |
On May 3, 6:35 am, noagbodjivic...@gmail.com 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? | | |
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. | | |
On May 3, 8:50 am, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
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):
? | | | me********@aol.com a écrit :
On May 2, 3:49 pm, Basilisk96 <basilis...@gmail.comwrote:
>>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 ? | | |
On May 2, 6:41 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
mensana...@aol.com a écrit :
On May 2, 3:49 pm, Basilisk96 <basilis...@gmail.comwrote:
>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 "<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. | | |
On May 2, 5:50 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
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.
| | |
In article <11**********************@h2g2000hsg.googlegroups. com>,
Dustan <Du**********@gmail.comwrote:
On May 2, 5:50 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
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" | | |
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 | | | me********@aol.com <me********@aol.comwrote:
...
>import gmpy gmpy.mpz(11)
mpz(11)
>gmpy.mpz('11',10)
mpz(11)
>gmpy.mpz(11,10)
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.
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 | | |
Steven D'Aprano <st***@REMOVEME.cybersource.com.auwrote:
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 | | |
On Wed, 02 May 2007 21:59:56 -0700, Alex Martelli wrote:
Steven D'Aprano <st***@REMOVEME.cybersource.com.auwrote:
>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 | | |
On May 2, 5:50 pm, Steven D'Aprano
<s...@REMOVE.THIS.cybersource.com.auwrote:
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.
| | |
On May 3, 5:59 am, a...@mac.com (Alex Martelli) wrote:
Steven D'Aprano <s...@REMOVEME.cybersource.com.auwrote:
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... | | |
In article <11**********************@n59g2000hsh.googlegroups .com>,
Ant <an****@gmail.comwrote:
On May 3, 5:59 am, a...@mac.com (Alex Martelli) wrote:
Steven D'Aprano <s...@REMOVEME.cybersource.com.auwrote:
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 :-) | | |
On May 2, 11:59 pm, a...@mac.com (Alex Martelli) wrote:
mensana...@aol.com <mensana...@aol.comwrote:
...
>>import gmpy
>>gmpy.mpz(11)
mpz(11)
>>gmpy.mpz('11',10)
mpz(11)
>>gmpy.mpz(11,10)
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.
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 "<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
>>gmpy.mpz('zzz',26)
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. | | | me********@aol.com wrote:
On May 2, 3:49 pm, Basilisk96 <basilis...@gmail.comwrote:
>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. | | |
On May 3, 2:03 pm, John Salerno <johnj...@NOSPAMgmail.comwrote:
mensana...@aol.com wrote:
On May 2, 3:49 pm, Basilisk96 <basilis...@gmail.comwrote:
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. | | |
This is a simple way to do it i think
s=hello
>>if(len(s)==0):
.... print "Empty"
.... else:
.... print s
....
hello | | |
On 4 May 2007 03:02:37 -0700, Jaswant <ma*************@gmail.comwrote:
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
>>>
| | |
On May 4, 5:02 am, Jaswant <mailme.gurpr...@gmail.comwrote:
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 "<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. | | |
Alex Martelli wrote:
Steven D'Aprano <st***@REMOVEME.cybersource.com.auwrote:
>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 | | |
On May 4, 1:31 pm, "Hamilton, William " <wham...@entergy.comwrote:
-----Original Message-----
From: mensana...@aol.com
On May 4, 5:02 am, Jaswant <mailme.gurpr...@gmail.comwrote:
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 "<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.
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
| | |
Larry Bates <la*********@websafe.comwrote:
...
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 | | |
On May 4, 9:19�pm, a...@mac.com (Alex Martelli) wrote:
Larry Bates <larry.ba...@websafe.comwrote:
* *...
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
| | |
On May 5, 12:19 pm, a...@mac.com (Alex Martelli) wrote:
Larry Bates <larry.ba...@websafe.comwrote:
...
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
| | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
6 posts
views
Thread by Ole Hanson |
last post: by
|
8 posts
views
Thread by monu |
last post: by
|
4 posts
views
Thread by AVL |
last post: by
|
9 posts
views
Thread by Søren M. Olesen |
last post: by
|
6 posts
views
Thread by Charlie@CBFC |
last post: by
|
26 posts
views
Thread by anonieko@hotmail.com |
last post: by
|
35 posts
views
Thread by Smithers |
last post: by
|
21 posts
views
Thread by Sami |
last post: by
| | | | | | | | | | |