Splitting strings - for a calculation
<p>I want to create a calculator with several functions. When the user inputs something such as "ADD3, DIVIDE4" it will output the answer. So I believe I will have to split the string to work on each part individually. 
So far all I can find is</p>

<pre><code>    numb= input("What is your first numb")
    calc = input("What is your calculation")
    calc.split(', ')
</code></pre>

<p>But I don't know how to work on each section once it has been split.
So for example if the first is numb = 5 and then its 'calc = ADD3, SUB4' the program will output 4. But then so when it gets more complicated with more calculations and things like DIVIDE it will still work.
Thank you</p>

<p>You could do something like this:</p>

<pre><code>numb = input("What is your first numb")
calc = input("What is your calculation")
ops = calc.split(', ')

for op in ops:
    if op.lower().startswith('add'):
        number = float(op[3:])
        numb += number
    elif op.lower().startswith('sub'):
        number = float(op[3:])
        numb -= number
    elif op.lower().startswith('divide'):
        number = float(op[6:])
        numb /= number
print(numb)
</code></pre>

<p>So what you do here is the following:</p>

<ul>
<li>Determine the operation ( op.lower().startswith(operation) ), .lower() makes it case insensitive</li>
<li>Find the number by selecting the rest of the string using string slicing (op[3:] or op[6:] )</li>
<li>Perform the operation on numb</li>
</ul>

<p>Note: This does the operations in the order they are inputted, not in the PEMDAS order of operations!</p>

python string split