I mean I have this string var:
mystr1 = "1==1 or 1==2"
mystr2 = "1==1 and 1==2"
if_logical_string(mystr1) must be True
if_logical_string(mystr2) must be False
How can I achieve this? Is there any lib to do so ? Thanks.
I mean I have this string var:
mystr1 = "1==1 or 1==2"
mystr2 = "1==1 and 1==2"
if_logical_string(mystr1) must be True
if_logical_string(mystr2) must be False
How can I achieve this? Is there any lib to do so ? Thanks.
mystr1 = "1==1 or 1==2"
mystr2 = "1==1 and 1==2"
# will output True
print(eval(mystr1))
# will output False
print(eval(mystr2))
If you have a mathematical expression, there is a more elegant way using Pyparsing. Check out this post: from Stackoverflow
Yes, you can use python's eval
function.
However, I would recommend having another approach... There's always another solution...