put multiple condition in if statement

A

Allerdyce.John

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.
 
A

Alex Martelli

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
 
S

Steven D'Aprano

Hi,

How can I put multiple condition in if statement?

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

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
 
D

Dennis Lee Bieber

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...

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

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

--
 
S

Steven D'Aprano

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:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'str' object is not callable
 
T

Terry Hancock

On 10 Mar 2006 21:12:57 -0800
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 (' ', ''):
 
B

Bruno Desthuilliers

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

Jim Segrave

Lesson one: Python is NOT C


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...


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.
 

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
474,289
Messages
2,571,448
Members
48,126
Latest member
ToneyChun2

Latest Threads

Top