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

G

Georg Brandl

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
 
E

eC

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
 
T

tac-tics

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 != ""
 
G

Grant Edwards

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 != ""):
<whatever>

However, the standard idiom for doing that in Python is

if str:
<whatever>
 
S

Shane Geiger

It is probably worth noting that the example is not executable code:
str() is a function.


sgeiger3@nycsr0002:~$ 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.....
whoah, str is a function!

/me pines for the day when all examples are executable code




Grant said:
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 != ""):
<whatever>

However, the standard idiom for doing that in Python is

if str:
<whatever>

--
Shane Geiger
IT Director
National Council on Economic Education
(e-mail address removed) | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
 
G

Grant Edwards

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 != ""):
<whatever>

However, the standard idiom for doing that in Python is

if str:
<whatever>

It is probably worth noting that the example is not executable code:
str() is a function.

Yea, sorry about that.
 
S

Steven D'Aprano

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.
.... 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?
<type 'str'>
 
B

Bruno Desthuilliers

(e-mail address removed) 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
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top