Skip to content

Instantly share code, notes, and snippets.

@amol9
Last active September 3, 2022 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amol9/26b7982e1f10427cafd22c68f36f0bef to your computer and use it in GitHub Desktop.
Save amol9/26b7982e1f10427cafd22c68f36f0bef to your computer and use it in GitHub Desktop.
Python script to subscript numbers in chemical formulae
p = "Hello world3 33"
import re
pfx = r"([a-zA-Z\)])"
p = re.sub(pfx + "0", r"\1₀", p)
p = re.sub(pfx + "1", r"\1₁", p)
p = re.sub(pfx + "2", r"\1₂", p)
p = re.sub(pfx + "3", r"\1₃", p)
p = re.sub(pfx + "4", r"\1₄", p)
p = re.sub(pfx + "5", r"\1₅", p)
p = re.sub(pfx + "6", r"\1₆", p)
p = re.sub(pfx + "7", r"\1₇", p)
p = re.sub(pfx + "8", r"\1₈", p)
p = re.sub(pfx + "9", r"\1₉", p)
print (p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment