The Python Oracle

Python convert a string to a logic operator

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Secret Catacombs

--

Chapters
00:00 Question
00:48 Accepted answer (Score 5)
01:10 Thank you

--

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

Accepted answer links:
[eval()]: http://docs.python.org/2/library/functio...
[must be cautious]: https://stackoverflow.com/a/1832957/2670...
[exec]: http://docs.python.org/2/reference/simpl...

--

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)