python - What's wrong with the IF some words IN a string -
this question has answer here:
how check string specific characters? find link useful. what's wrong codes?
string = "a17_b_c_s.txt" if ("m.txt" , "17") in string: print true else: print false
and answer comes out
true
this because and
evaluates 17
in stringlist
. and
evaluates 17 because of short circuiting.
>>> "m.txt" , "17" '17'
python evaluates non empty strings value true
. hence, m.txt
evaluates true
, value of expression depends on second value returned(17
) , found in stringlist
. (why? when and
performed true
value, value of expression depends on second value, if it's false
, value of expression false
, else, it's true
.)
you need change expression
if "m.txt" in stringlist , "17" in stringlist: #...
or use builtin all()
if all(elem in stringlist elem in ["m.txt", "17"]): #...
Comments
Post a Comment