put multiple condition in if statement
Jim Segrave
jes at nl.demon.net
Sun Mar 12 10:30:30 EST 2006
In article <qhp412tba18h050i1m7974n83lih4hefa3 at 4ax.com>,
Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote:
>On 10 Mar 2006 21:12:57 -0800, Allerdyce.John at 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 (jes at jes-2.demon.nl)
More information about the Python-list
mailing list