0

I came across this question for the interview. I had to convert the string to Boolean and return the Boolean value

For ex s="3>1>5" => false

I tried using

ans= bool(s) 

But I always get the answer as True even though the above example gives False if we don't pass it as a string

2
  • 3
    If you read the docs for bool() you'll see that it doesn't evaluate the contents of a string passed to it. Commented Jun 23, 2020 at 17:04
  • If you are not allowed to use eval, then you have to do full-scale parsing - unless the expressions in the strings have very simple, regular structure. Commented Jun 23, 2020 at 17:13

1 Answer 1

5

You must be looking for eval(string)

>>> eval("3>1>5") False 
Sign up to request clarification or add additional context in comments.

2 Comments

Mandatory caveat that eval really is dangerous.
@jarmod indeed, feel free to edit my answer if you'd like to add that

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.