The Python Oracle

Python convert a string to a logic operator

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Underwater World

--

Chapters
00:00 Python Convert A String To A Logic Operator
00:28 Accepted Answer Score 5
00:44 Thank you

--

Full question
https://stackoverflow.com/questions/2213...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #logicaloperators #booleanexpression #booleanoperations #stringconversion

#avk47



ACCEPTED ANSWER

Score 5


You can use eval() function:

print eval(x)

Note that you must be cautious while using it.

Edit:

As @PriyankPatel mentioned, another way would be using exec:

exec("print " + x)