Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Strings and arithmetic operations

people. Can you please tell me how can I turn a string into arithmetic operation.

For example:

string_with_operation = "(23 + 3) / 4"

usual_operation = (23 + 3) / 4

I want a string_with_operation to be the same as usual_operation

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried turn string_with_operation into an integer and a float but as expected it caused an error.

>Solution :

In python can use eval() function

string_with_operation = "(23 + 3) / 4"

result = eval(string_with_operation)

print(result)

This is a good tutorial
https://realpython.com/python-eval-function/#:~:text=You%20can%20use%20the%20built,it%20as%20a%20Python%20expression.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading