Oct-04-2023, 03:08 PM
This is related to your other post? https://python-forum.io/thread-40853.html
You cannot "convert" string_wrong into correct. string_wrong is a str and correct is list containing the return value of a function call. What you could do is evaluate the python expresssion in string_wrong. It might return the same result.
You cannot "convert" string_wrong into correct. string_wrong is a str and correct is list containing the return value of a function call. What you could do is evaluate the python expresssion in string_wrong. It might return the same result.
correct = [5 + 2] wrong = "[5 + 2]" resolved = eval(wrong) print(correct) print(wrong) print(resolved)
Output:[7]
[5 + 2]
[7]
Be aware that eval(expression) executes the expression, any valid expression. The expression could add two numbers, or it could format your hard drive. Only use eval() if you know what expressions will be evaluated.