<p>As you mentioned, rdKit does not accept a chemical formula as input. The main reason for this are probably isomers. You could however use rdKit to initiate all the atoms, get their mass and add them together.</p>

<pre><code>import re
from rdkit import Chem


def get_mass(formula):

    parts = re.findall("[A-Z][a-z]?|[0-9]+", formula)
    mass = 0

    for index in range(len(parts)):
        if parts[index].isnumeric():
            continue

        atom = Chem.Atom(parts[index])
        multiplier = int(parts[index + 1]) if len(parts) &gt; index + 1 and parts[index + 1].isnumeric() else 1
        mass += atom.GetMass() * multiplier

    return mass
</code></pre>

<p>Then...</p>

<pre><code>&gt;&gt;&gt; get_mass("C9H15N4O8P")
338.21299999999997
</code></pre>

CC BY-SA 4.0
2019-08-26T04:50:10.333Z
2019-08-26T04:50:10.333Z
Zac R.
<p>I tried to adapt this approach within rdKit to calculate exact mass (for mass spectrometry), but was not successful.  </p>

<p>This library (<a href="https://pypi.org/project/molmass/" rel="nofollow noreferrer">https://pypi.org/project/molmass/</a>) appears to be able to do this:</p>

<pre><code>from molmass import Formula
f = Formula('C7H11N3O2')
f.formula
f.isotope.mass
'169.08512660696'
</code></pre>

CC BY-SA 4.0
2024-08-01T11:58:49.997Z
be cautious with molmass because it admits that it does not include the mass defect in the calculations so for large molecules there will be a sizable inaccuracy.
glycoaddict
CC BY-SA 4.0
2020-02-14T14:12:53.523Z
2020-02-14T14:12:53.523Z
Crath
<p>Can I calculate the molecular mass from a formula like this <code>C9H15N4O8P</code> with Python e.g. using <code>rdkit</code>? I think <code>rdkit.Chem.Descriptors.MolWt</code> needs a molecule object. Though, to generate that I would need e.g. a SMILES string. Is there any other python module that can do the trick?</p>

<p>I found this: <a href="http://verahill.blogspot.com/2012/10/molecular-weight-calculator-in-python.html" rel="nofollow noreferrer">http://verahill.blogspot.com/2012/10/molecular-weight-calculator-in-python.html</a></p>

<python>
Calculate molecular mass from chemical formula C9H15N4O8P with Python?
# Calculate molecular mass from chemical formula C9H15N4O8P with Python?

(Asked by: username_0 on 2019-01-21)

Can I calculate the molecular mass from a formula like this `C9H15N4O8P` with Python e.g. using `rdkit`? I think `rdkit.Chem.Descriptors.MolWt` needs a molecule object. Though, to generate that I would need e.g. a SMILES string. Is there any other python module that can do the trick?

I found this: <http://verahill.blogspot.com/2012/10/molecular-weight-calculator-in-python.html>

**Tags:** python

---

## 2 answers

### Answer by username_2 on 2020-02-14. Score: 3 | Accepted answer

I tried to adapt this approach within rdKit to calculate exact mass (for mass spectrometry), but was not successful.  
This library (https://pypi.org/project/molmass/) appears to be able to do this:
from molmass import Formula
f = Formula('C7H11N3O2')
f.formula
f.isotope.mass
'169.08512660696'

> **Comments:**
> **username_3**: be cautious with molmass because it admits that it does not include the mass defect in the calculations so for large molecules there will be a sizable inaccuracy.  

---

### Answer by username_1 on 2019-08-26. Score: 3

As you mentioned, rdKit does not accept a chemical formula as input. The main reason for this are probably isomers. You could however use rdKit to initiate all the atoms, get their mass and add them together.
import re
from rdkit import Chem


def get_mass(formula):

    parts = re.findall("[A-Z][a-z]?|[0-9]+", formula)
    mass = 0

    for index in range(len(parts)):
        if parts[index].isnumeric():
            continue

        atom = Chem.Atom(parts[index])
        multiplier = int(parts[index + 1]) if len(parts) > index + 1 and parts[index + 1].isnumeric() else 1
        mass += atom.GetMass() * multiplier

    return mass

Then...
>>> get_mass("C9H15N4O8P")
338.21299999999997


---

