-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.

0

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, you can use python's eval function.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.