-2

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.

chickensoup
  • 334
  • 1
  • 17

2 Answers2

2
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

Dan Ionescu
  • 3,135
  • 1
  • 12
  • 17
1

Yes, you can use python's eval function.

However, I would recommend having another approach... There's always another solution...

Léopold Houdin
  • 1,515
  • 13
  • 18