Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print("string1" or "string2" in string) does not give Boolean result

why does print("Lorem" and "aliqua" in string ) Gives True. A Boolean,

But print("Lorem" or "aliqua" in string ) Gives 'Lorem'. A String

string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"

print("Lorem" and "aliqua" in string )
>>> True

print("Lorem" or "aliqua" in string )
>>> Lorem
like image 751
Ankit Chaurasia Avatar asked Oct 28 '25 22:10

Ankit Chaurasia


2 Answers

Try:

print("Lorem" in string and "aliqua" in string )

And

print("Lorem" in string or "aliqua" in string )

Explanation: The condition in string will always be true as it checks string is non empty.

>>> if "harsha":
...   print("hi")
...
hi
>>> if "":
...   print("hi")
...
<<No output>>
like image 70
Harsha Biyani Avatar answered Oct 31 '25 10:10

Harsha Biyani


In your second example, the string "Lorem" evaluates to True and is being printed without checking if "aliqua" appears. Try searching for both.

any([x in string for x in ["Lorem", "aliqua"]])
>> True
like image 23
JDR Avatar answered Oct 31 '25 12:10

JDR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!