+ 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.
6 odpowiedzi
+ 3
You can use conditional statements {if-else} for it.
0
Use
Let num = str.split('+'); # return an array
Then add the num by a loop
0
eval('2+3')
Gives the output 5 where 2+3 is a string and is "evaluated" hence the method name eval()
0
Use python built in operator module and a dict to get corresponding operand
- 1
Have you tried to use escape character on the string?