(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 !-)
"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