N
noagbodjivictor
How to check if a string is empty in python?
if(s == "") ??
if(s == "") ??
How to check if a string is empty in python?
if(s == "") ??
A simple
if s:
print "not empty"
else:
print "empty"
will do.
How to check if a string is empty in python?
if(s == "") ??
How to check if a string is empty in python?
if(s == "") ??
How to check if a string is empty in python?
if(s == "") ??
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.
How to check if a string is empty in python?
if(s == "") ??
How do you know that s is a string?
(e-mail address removed) a écrit :
Why do you want to know if it's a string ?
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.
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... ?
for c in s:
raise "it's not empty"
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.
Steven D'Aprano said:
They're actually deprecated, not depreciated.
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.
They're actually deprecated, not depreciated.
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.