How can i compare a string which is non null and empty
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? -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
[EMAIL PROTECTED] 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? What about if str != "": pass ? -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
[EMAIL PROTECTED] 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". This also includes the case that "str" may not only be an empty string, but also None. Georg -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
On Apr 2, 12:22 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
str != "" returns true if str is NOT the empty string. str is not None returns true if str is null (or None as it's called in python). To check to make sure a string is nonnull and nonempty, do: str is not None and str != "" -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
On 2007-04-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > how can i compare a string which is non null and empty? [...] > In java,I do this: > if (str != null) && (!str.equals("")) > > how can i do that in python? If you want to litterally do that, it's if (str != None) and (str != ""): However, the standard idiom for doing that in Python is if str: -- Grant Edwards grante Yow! I'm RELIGIOUS!! I at love a man with a visi.comHAIRPIECE!! Equip me with MISSILES!! -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
It is probably worth noting that the example is not executable code: str() is a function. [EMAIL PROTECTED]:~$ python Python 2.4.4c0 (#2, Jul 30 2006, 15:43:58) [GCC 4.1.2 20060715 (prerelease) (Debian 4.1.1-9)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> if str: print "whoah, str is a function!" ... whoah, str is a function! >>> /me pines for the day when all examples are executable code Grant Edwards wrote: On 2007-04-01, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: how can i compare a string which is non null and empty? [...] In java,I do this: if (str != null) && (!str.equals("")) how can i do that in python? If you want to litterally do that, it's if (str != None) and (str != ""): However, the standard idiom for doing that in Python is if str: -- Shane Geiger IT Director National Council on Economic Education [EMAIL PROTECTED] | 402-438-8958 | http://www.ncee.net Leading the Campaign for Economic and Financial Literacy begin:vcard fn:Shane Geiger n:Geiger;Shane org:National Council on Economic Education (NCEE) adr:Suite 215;;201 N. 8th Street;Lincoln;NE;68508;United States email;internet:[EMAIL PROTECTED] title:IT Director tel;work:402-438-8958 x-mozilla-html:FALSE url:http://www.ncee.net version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
On 2007-04-02, Shane Geiger <[EMAIL PROTECTED]> wrote: >>> how can i compare a string which is non null and empty? >>> >> [...] >> >>> In java,I do this: >>> if (str != null) && (!str.equals("")) >>> >>> how can i do that in python? >> >> If you want to litterally do that, it's >> >> if (str != None) and (str != ""): >> >> >> However, the standard idiom for doing that in Python is >> >> if str: >> > > It is probably worth noting that the example is not executable code: > str() is a function. Yea, sorry about that. -- Grant Edwards grante Yow! Now I'm concentrating at on a specific tank battle visi.comtoward the end of World War II! -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
On Mon, 02 Apr 2007 01:35:17 +0200, Georg Brandl wrote: > [EMAIL PROTECTED] 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 -- Steven D'Aprano -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
[EMAIL PROTECTED] a écrit : > Hi, > > how can i compare a string which is non null and empty? Compare with what ?-) > > 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? > 1/ don't use 'str' as an identifier as it will shadow the builtin str type. 2/ just test: if some_str: # code here In a boolean context, both the None object (the closer equivalent to Java's 'null') and an empty string will eval to False (FWIW, so will do an empty list, an empty tuple, an empty set, an empty dict, and a numeric zero). FWIW, Python allows (and encourage where it makes sens) operator overloading. cf: http://docs.python.org/ref/specialnames.html HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
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' ... lst = ['a','',5,(5,6),{'a':5}] for item in lst: ... stringTest(item) ... Not Empty String Empty String value not String Type value not String Type value not String Type -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
On Mon, 02 Apr 2007 13:48:30 -0700, Steven Howe wrote: > 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' Too complicated. You don't need to import StringType, nor do you need to explicitly ask for the length. Worse, you reject anything that isn't *precisely* a string. Other string classes, like Unicode and subclasses, are wrongly rejected. Try this instead: def stringTest(x): if isinstance(x, basestr): if x: print "Not an empty string." else: print "An empty string." else: print "Not a string." Here's an even shorter version, saving one line. def stringTest(x): if not isinstance(x, basestring): print "Not a string." elif x: print "Not an empty string." else: print "An empty string." And here's an even shorter version. def stringTest(x): if not isinstance(x, basestring): print "Not a string." else: print {True: "Not a", False: "A"}[bool(x)] + "n empty string." Notice, though, that shorter code is not always better. -- Steven D'Aprano -- http://mail.python.org/mailman/listinfo/python-list
Re: How can i compare a string which is non null and empty
Steven Howe a écrit : (nb : original post reinserted) > [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list