How to check if a string is empty in python?

  • Thread starter noagbodjivictor
  • Start date
A

attn.steven.kuo

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

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

John Machin

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

Steven D'Aprano

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

John Machin

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):
?
 
M

mensanator

(e-mail address removed) a écrit :



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

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

Dustan

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... ?
 
R

Roy Smith

Dustan said:
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... ?

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

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

Alex Martelli

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
 
A

Alex Martelli

Steven D'Aprano said:
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
 
S

Steven D'Aprano

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

Dustan

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.
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top