Sunday 15 March 2015

For loop python multiple checks -


If I have the wire like the following:

  my_string (0) = your future Looks good My_string (1) = Your future does not look good   

I want to print both lines with:

  For ding in my_string: If 'future' or 'future' in the string: print 'success!'   

works for the first condition with my if loop future , however, the future What is the reason with the other comparison does not work?

Use:

  If the 'future' string or ' In future 'string':   

or simply:

  if 'future' string.lower () in   

Why it's unsuccessful:

  In the 'future' or 'future' string:   

is actually equivalent:

 < Code> True or (in the 'Future' string) # bool ('FUTURE') - & gt; True   

As the first condition is always true , the next situation is never taken. Actually your position always does not matter to any string true .

A temporary string is always found in the True python and the correct value is as accurate as the or operation short-circuits.

  & gt; & Gt; & Gt; Strs1 = "Your future does not look good." & Gt; & Gt; & Gt; Strs2 = "Your future looks good." & Gt; & Gt; & Gt; 'Future' or 'future' strs1 'future' & gt; & Gt; & Gt; 'Furar' or 'future' strs1 'flower' & gt; & Gt; & Gt; 'Furar' or 'Cat' in Struss 1 'Furar' & gt; & Gt; & Gt; '' Or 'cat' strs1 # is a false value in the empty string, false # so now it checks the next position   

Note:

  And gt; & Gt; & Gt; 'Futurature' is true in 'Future';   

is true , as the in operator does not match the exact wording sub-strings. / P>

Use regex or str.split to control such cases.

No comments:

Post a Comment