How can i compare a string which is non null and empty
Steven D'Aprano
steve at REMOVEME.cybersource.com.au
Mon Apr 2 04:59:18 EDT 2007
On Mon, 02 Apr 2007 01:35:17 +0200, Georg Brandl wrote:
> Plissken.s at gmail.com schrieb:
>> Hi,
>>
>> how can i compare a string which is non null and empty?
>>
>>
>> i look thru the string methods here, but cant find one which does it?
>>
>> http://docs.python.org/lib/string-methods.html#string-methods
>>
>> In java,I do this:
>> if (str != null) && (!str.equals("")) ....
>>
>> how can i do that in python?
>
> Strings cannot be "null" in Python.
>
> If you want to check if a string is not empty, use "if str".
I tried that, and I get something unexpected.
>>> if str:
... print "What's going on here?"
... else:
... print "An empty string."
...
What's going on here?
> This also includes the case that "str" may not only be an empty
> string, but also None.
What about the case where str hasn't been shadowed and is a built-in type?
>>> str
<type 'str'>
--
Steven D'Aprano
More information about the Python-list
mailing list