0

Peharps I have a str in Python like "8+3" or even more difficult like "sin(30)" or others. How to convert then from type of str to calculate. For eхample if i have str like "8+3" it must calculate like int numbers 8+3 = 11, or if i have str like "sin(30)" it must calculate math.sin(30).

Alek H.
  • 119
  • 1
  • 2
  • 10

2 Answers2

0
my_string = "8+3"
my_result = eval(my_string)
Ludovic Viaud
  • 202
  • 1
  • 5
0

I would use regex to pull out the number(s), and then do a check to see what operation to preform on said number(s).

Freelancer
  • 86
  • 1
  • 9