How can i compare a string which is non null and empty

eC ericcoetzee at gmail.com
Sun Apr 1 19:50:35 EDT 2007


On Apr 2, 12:22 am, "Plisske... at gmail.com" <Plisske... at gmail.com>
wrote:
> 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?
The closest to null in python is None.
Do you initialise the string to have a value of None? eg myStr = None

if so, you can test with
if myStr == None:
   dosomething...

But you might find that you do not need to use None - just initialise
the string as empty eg myStr = ''
and then test for
if myStr != '':
or even simpler

if myStr:
  dosomething

btw. dont use 'str' - its a built in function of python




More information about the Python-list mailing list