Question: How can I execute a calculation in python (2.7) and capture the output in the variable 'results'?

**Input:** 

```
let calculation = '(3*15)/5'
let formatting  = '\\%2d'
let grouping = '0'

exe "python -c \"import math, locale; locale.format(formatting, calculation, grouping)\""

```

and capture the output in a variable `results`

**Output:**

results = '9'

ps:  

Is there also a possibility in python to wrap the first part of my calculation, something like the eval() function in vim?  

p.e. 

```
  let command = 'python -c \"import math, locale\"'
  exe "eval(command); locale.format(formatting, calculation, grouping)"

```

Answer: 
Hmm… `:let foo = (3*15)/15`.

Anyway, you can capture the output of an external command with `:let foo = system('mycommand')`:

```
:let foo = system("python -c 'print (15*3)/5'")

```

It's up to you to put the string together.
I ever wrote a script to do number calculation in vim. It supports three expression evaluation engines: `GNU bc, vimscript and python`.

It is called `HowMuch` : <https://github.com/sk1418/HowMuch>

For the python part, you could check here:

<https://github.com/sk1418/HowMuch/blob/master/autoload/HowMuch.vim#L285>

hope it helps.