By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,742 Members | 1,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,742 IT Pros & Developers. It's quick & easy.

put multiple condition in if statement

P: n/a
Hi,

How can I put multiple condition in if statement?

I try this, but I can't get that to work.

if ( (str != " ") && (str != "") ):

Any help is appreciated.

Thank you.

Mar 11 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
<Al************@gmail.com> wrote:
Hi,

How can I put multiple condition in if statement?

I try this, but I can't get that to work.

if ( (str != " ") && (str != "") ):


Why would you WANT to conjoin two instances of the SAME check...?!

Anyway, use 'and' -- there's no such thing as '&&' in the Python
language!
Alex
Mar 11 '06 #2

P: n/a
On Fri, 10 Mar 2006 21:12:57 -0800, Allerdyce.John wrote:
Hi,

How can I put multiple condition in if statement?

I try this, but I can't get that to work.

if ( (str != " ") && (str != "") ):


if ( (str != " ") && (str != "") ):

File "<stdin>", line 1
if ( (str != " ") && (str != "") ):
^
SyntaxError: invalid syntax

Hmmm... it looks like && is a syntax error in Python. That's probably
because Python isn't C or Java or whatever language you're trying to
program in :-)

Python has operators and, or, not that will do what you are trying to do.

You may want to read these first:

http://docs.python.org/ref/bitwise.html
http://docs.python.org/ref/Booleans.html

--
Steven.

Mar 11 '06 #3

P: n/a
On Fri, 10 Mar 2006 21:47:33 -0800, Alex Martelli wrote:
<Al************@gmail.com> wrote:
Hi,

How can I put multiple condition in if statement?

I try this, but I can't get that to work.

if ( (str != " ") && (str != "") ):


Why would you WANT to conjoin two instances of the SAME check...?!


No, the first is checking for str equal to a space character, the second
for the empty string. You need to get those glasses checked *grin*
Note to the original poster: the more "pythonic" way of doing this check
will be:

if s and s.strip():
# do something with non-empty s
else:
# s is either empty or all whitespace

Don't use "str" as a variable name, as it will over-ride the built-in type
str. This will eventually cause you problems, when you try something like
this:
str(5) '5' str = "Hello world" # shadow the built-in
str(5)

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'str' object is not callable

--
Steven.

Mar 11 '06 #4

P: n/a
On 10 Mar 2006 21:12:57 -0800
Al************@gmail.com wrote:
How can I put multiple condition in if statement?
I try this, but I can't get that to work.

if ( (str != " ") && (str != "") ):


if s!=' ' and s!='':

1) logical operators are words
2) don't overload standard type names

this is actually better style:

if s not in (' ', ''):

--
Terry Hancock (ha*****@AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com

Mar 11 '06 #5

P: n/a
Al************@gmail.com a écrit :
Hi,

How can I put multiple condition in if statement?
With "and"s and "or"s.

I try this, but I can't get that to work.

if ( (str != " ") && (str != "") ):
<joke type="lame">
FWIW, I would not hope a type to be equal to a string !-)
</joke>
Any help is appreciated.


"doesn't work" is the most useless possible description of a problem. As
a general rule, please:
- post the minimal running code that exhibit the problem
- clearly state the result you hoped and the result you got.

Also, don't use builtin names as identifier. "str" is the string type,
so using "str" as an identifier will shadow the str type.

And, finally, you don't even need multiple conditions here - this should
be enough (using 'my_string' instead of 'str'):

if my_string.strip() :
...

In Python, an empty sequence (strings, lists, tuples, ...), an empty
mapping (dict, ...), the integer 0 and the float 0.0 are all evalued to
False in a boolean expression. So any non-empty string evals to True.
The method strip() of type str returns a copy of the string with leading
and trailing whitespaces removed, so " ".strip() will return "".

HTH
Mar 11 '06 #6

P: n/a
In article <qh********************************@4ax.com>,
Dennis Lee Bieber <wl*****@ix.netcom.com> wrote:
On 10 Mar 2006 21:12:57 -0800, Al************@gmail.com declaimed the
following in comp.lang.python:
How can I put multiple condition in if statement?

Lesson one: Python is NOT C
I try this, but I can't get that to work.

if ( (str != " ") && (str != "") ):


Lesson two: an empty (aka, null) string is "false", and a non-empty
string is "true", so that gives us

Lesson three: " " is not equal to " " or " " so you
might want to do strip whitespace first...
"" == " "False " " == " "False "".strip() == " ".strip()True


So... Your mixed C/Python simplifies to just...

if str.strip(): # after removing leading/trailing spaces,
# if not empty, do something


str = " "
and
str = "\t"
fail with your substitution - the OP was looking only for strings
containing one space or empty strings. Tab characters and multi-spaces
would not match.

--
Jim Segrave (je*@jes-2.demon.nl)

Mar 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.