{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Formatting numbers, precision, output"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TLDR\n",
    "\n",
    "### Define some constants"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "pi = 3.14159265358979323846264338 \n",
    "Na = 6.0221409e+26                 # #/kmol\n",
    "atm = 101.325                      # kPa\n",
    "kB  = 1.3806485279E-23             # J/K\n",
    "Rg  = 8314                         # J/kmol*K"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Format 'em\n",
    "* Use a formatted string\n",
    "    * ```f\"some text, value of var inserted here: {var}\"```\n",
    "    * That is, ```{var}``` inserts the value of variable ```var```\n",
    "    * Can have multiple ```{var}``` in a single string.\n",
    "* Format the variable like this ```{var:w.pt}``` \n",
    "    * The format is after the ```:```\n",
    "    * ```w``` is the width the variable should take (optional)\n",
    "    * ```.p``` is the precision (optional)\n",
    "    * ```t``` is the type (optional)\n",
    "        * ```e``` for scientific 6.02E23\n",
    "        * ```f``` for floating point number 3.14\n",
    "        * ```d``` for integer 101\n",
    "        * ```s``` for string\n",
    "        * etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Na  =        6.0221409e+26 #/kmol\n",
      "atm =              101.325 kPa\n",
      "Rg  =                 8314 J/kmol*K\n"
     ]
    }
   ],
   "source": [
    "print(    f\"Na  = {Na:20.7e} #/kmol\" )\n",
    "print(    f\"atm = {atm:20.3f} kPa\"   )\n",
    "print(    f\"Rg  = {Rg:20d} J/kmol*K\" )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Details"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "There are three main formatting methods. \n",
    "* % formatting\n",
    "* str.format()\n",
    "* f-strings\n",
    "\n",
    "### Here, we'll use the newer f-string method.\n",
    "\n",
    "### Useful links:\n",
    "* [f-string introduction](https://realpython.com/python-f-strings/) with a brief summary of the other methods.\n",
    "* [Overview](https://realpython.com/python-string-formatting/) of the three methods.\n",
    "* The str.format() and f-string methods have a lot of similiarities.\n",
    "    * [This link](https://www.geeksforgeeks.org/python-format-function/) gives a good description of the str.format() method which is good for understanding.\n",
    "* [This site](https://pyformat.info) directly compares the older % formatting to the str.format() methods, and gives lots of good examples.\n",
    "* Once you already know what is going on, [this short site](https://python-reference.readthedocs.io/en/latest/docs/functions/format.html) gives a pretty complete summary of format codes and options.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction, examples\n",
    "* Here, we focus on formatting numbers.\n",
    "* Keep things simple, the above links give more details.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Crude printing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of pi is 3.141592653589793\n"
     ]
    }
   ],
   "source": [
    "print(  \"The value of pi is\", pi   )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### f-strings\n",
    "* This stands for formatted string.\n",
    "* Write the string with a leading f\n",
    "    * ```f\"My string\"```\n",
    "* Insert variables into the string using {varName}.\n",
    "    * ```f\"My string {varName}\"```\n",
    "    * The ```{varName}``` is replaced with the value of the variable."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of pi is 3.141592653589793\n"
     ]
    }
   ],
   "source": [
    "print(  f\"The value of pi is {pi}\"   )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "kB=1.3806485279e-23 and Na=6.0221409e+26\n"
     ]
    }
   ],
   "source": [
    "print(  f\"kB={kB} and Na={Na}\"   )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "k=1.3806485279e-23 and Na=6.0221409e+26\n"
     ]
    }
   ],
   "source": [
    "p = f\"k={kB} and Na={Na}\"\n",
    "print(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Generic python expressions can go inside ```{ }```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pi^2 = 9.869604401089358\n"
     ]
    }
   ],
   "source": [
    "print(   f\"pi^2 = {pi**2}\"   )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Formating numbers\n",
    "* Format as ```{var:t}``` where ```t``` is one of \n",
    "    * ```e``` or ```E``` for scientific, like 6.02E23\n",
    "    * ```f``` for floating point, like 3.14\n",
    "    * ```d``` for integers, like 101 (d is for decimal number, as opposed to, say, binary)\n",
    "    * ```g``` for general number (python chooses), \n",
    "    * (also, ```s``` for string).\n",
    "    * other types available, see links above.\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rg = 8314\n",
      "Rg = 8314.000000\n",
      "Rg = 8.314000e+03\n"
     ]
    }
   ],
   "source": [
    "print(    f\"Rg = {Rg:d}\"   )\n",
    "print(    f\"Rg = {Rg:f}\"   )\n",
    "print(    f\"Rg = {Rg:e}\"   )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Precision\n",
    "\n",
    "* Format as ```{var:.5t}```, where the ```.5``` means 5 decimal places."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pi = 3.14159\n",
      "pi = 3.14159e+00\n"
     ]
    }
   ],
   "source": [
    "print(    f\"pi = {pi:.5f}\"    )\n",
    "print(    f\"pi = {pi:.5e}\"    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Width\n",
    "* Specify a fixed width of a number.\n",
    "* ```{var:w.pt}``` where ```w``` is the full width and ```p``` is the precision."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pi  =              3.14159\n",
      "Na  =          6.02214e+26\n",
      "kB  =          1.38065e-23\n",
      "atm =            101.32500\n",
      "Rg  =                 8314\n"
     ]
    }
   ],
   "source": [
    "print(    f\"pi  = {pi:20.5f}\"    )\n",
    "print(    f\"Na  = {Na:20.5e}\"    )\n",
    "print(    f\"kB  = {kB:20.5e}\"    )\n",
    "print(    f\"atm = {atm:20.5f}\"   )\n",
    "print(    f\"Rg  = {Rg:20d}\"      )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### The format codes can be stored as variables\n",
    "* If you have lots of print statements you only have to change ```w```, ```p```, and ```t``` below, instead of changing every print statement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Na  =          6.02214e+26\n"
     ]
    }
   ],
   "source": [
    "w = 20\n",
    "p = 5\n",
    "t = 'e'\n",
    "print(    f\"Na  = {Na:{w}.{p}{t}}\"    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Right, left, center\n",
    "* ```<``` for left\n",
    "* ```>``` for right\n",
    "* ```^``` for center\n",
    "* ```{var:<w.pt}```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "kB = 1.3806e-23           J/K\n",
      "kB =           1.3806e-23 J/K\n",
      "kB =      1.3806e-23      J/K\n"
     ]
    }
   ],
   "source": [
    "print(    f\"kB = {kB:<20.4e} J/K\"    )\n",
    "print(    f\"kB = {kB:>20.4e} J/K\"    )\n",
    "print(    f\"kB = {kB:^20.4e} J/K\"    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [Click here for more options](https://python-reference.readthedocs.io/en/latest/docs/functions/format.html)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
