How can i compare a string which is non null and empty
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Tue Apr 3 15:50:34 EDT 2007
Steven Howe a écrit :
(nb : original post reinserted)
> Plissken.s at gmail.com a écrit :
>> how can i compare a string which is non null and empty?
>>
(snip)
>> In java,I do this:
>> if (str != null) && (!str.equals("")) ....
>>
>> how can i do that in python?
>
> how about just testing it's length?
> >>> from types import StringType
>
> def stringTest(x):
> ... if type(x) == StringType:
> ... if len(x) == 0:
> ... print 'Empty String'
> ... else:
> ... print 'Not Empty String'
> ... else:
> ... print 'value not String Type'
> ...
(snip)
I'm sorry to say this is a excellent candidate for the DailyWTF. It's a
perfect exemple of uselessly overcomplificated non-idiomatic code. And
it doesn't even answers the OP's question.
The pythonic translation of the OP's java snippet is :
if some_str:
# code here
This will test that some_str is neither the None object (closer Python
equivalent to Java's null) nor an empty string. Whic is what the OP
asked for.
HTH
More information about the Python-list
mailing list