How can I convert string '+' to operator + in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I convert string '+' to operator + in python?

For a simple calculator program, if i take input of the operator between two numbers in a string, how can I convert the the string '+' into + so that I can print the output.

30th Oct 2020, 6:13 AM
KOUSHIK L
KOUSHIK L - avatar
6 Answers
30th Oct 2020, 6:22 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
You can use conditional statements {if-else} for it.
30th Oct 2020, 6:15 AM
Arsenic
Arsenic - avatar
0
Use Let num = str.split('+'); # return an array Then add the num by a loop
30th Oct 2020, 10:27 AM
Abhay
Abhay - avatar
0
eval('2+3') Gives the output 5 where 2+3 is a string and is "evaluated" hence the method name eval()
30th Oct 2020, 1:40 PM
Shubhank Gupta
Shubhank Gupta - avatar
0
Use python built in operator module and a dict to get corresponding operand
9th Sep 2021, 6:55 AM
Shivendra Rajawat
Shivendra Rajawat - avatar
- 1
Have you tried to use escape character on the string?
30th Oct 2020, 8:24 AM
Nyambi Ndep Arrey
Nyambi Ndep Arrey - avatar