#!/usr/bin/env python # coding: utf-8 # # Formatting numbers, precision, output # ## TLDR # # ### Define some constants # In[86]: pi = 3.14159265358979323846264338 Na = 6.0221409e+26 # #/kmol atm = 101.325 # kPa kB = 1.3806485279E-23 # J/K Rg = 8314 # J/kmol*K # ### Format 'em # * Use a formatted string # * ```f"some text, value of var inserted here: {var}"``` # * That is, ```{var}``` inserts the value of variable ```var``` # * Can have multiple ```{var}``` in a single string. # * Format the variable like this ```{var:w.pt}``` # * The format is after the ```:``` # * ```w``` is the width the variable should take (optional) # * ```.p``` is the precision (optional) # * ```t``` is the type (optional) # * ```e``` for scientific 6.02E23 # * ```f``` for floating point number 3.14 # * ```d``` for integer 101 # * ```s``` for string # * etc. # In[109]: print( f"Na = {Na:20.7e} #/kmol" ) print( f"atm = {atm:20.3f} kPa" ) print( f"Rg = {Rg:20d} J/kmol*K" ) # ## Details # # There are three main formatting methods. # * % formatting # * str.format() # * f-strings # # ### Here, we'll use the newer f-string method. # # ### Useful links: # * [f-string introduction](https://realpython.com/python-f-strings/) with a brief summary of the other methods. # * [Overview](https://realpython.com/python-string-formatting/) of the three methods. # * The str.format() and f-string methods have a lot of similiarities. # * [This link](https://www.geeksforgeeks.org/python-format-function/) gives a good description of the str.format() method which is good for understanding. # * [This site](https://pyformat.info) directly compares the older % formatting to the str.format() methods, and gives lots of good examples. # * 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. # # # ## Introduction, examples # * Here, we focus on formatting numbers. # * Keep things simple, the above links give more details. # # ### Crude printing # In[47]: print( "The value of pi is", pi ) # ### f-strings # * This stands for formatted string. # * Write the string with a leading f # * ```f"My string"``` # * Insert variables into the string using {varName}. # * ```f"My string {varName}"``` # * The ```{varName}``` is replaced with the value of the variable. # In[48]: print( f"The value of pi is {pi}" ) # In[49]: print( f"kB={kB} and Na={Na}" ) # In[50]: p = f"k={kB} and Na={Na}" print(p) # ### Generic python expressions can go inside ```{ }``` # In[51]: print( f"pi^2 = {pi**2}" ) # ### Formating numbers # * Format as ```{var:t}``` where ```t``` is one of # * ```e``` or ```E``` for scientific, like 6.02E23 # * ```f``` for floating point, like 3.14 # * ```d``` for integers, like 101 (d is for decimal number, as opposed to, say, binary) # * ```g``` for general number (python chooses), # * (also, ```s``` for string). # * other types available, see links above. # # In[93]: print( f"Rg = {Rg:d}" ) print( f"Rg = {Rg:f}" ) print( f"Rg = {Rg:e}" ) # ### Precision # # * Format as ```{var:.5t}```, where the ```.5``` means 5 decimal places. # In[64]: print( f"pi = {pi:.5f}" ) print( f"pi = {pi:.5e}" ) # ### Width # * Specify a fixed width of a number. # * ```{var:w.pt}``` where ```w``` is the full width and ```p``` is the precision. # In[94]: print( f"pi = {pi:20.5f}" ) print( f"Na = {Na:20.5e}" ) print( f"kB = {kB:20.5e}" ) print( f"atm = {atm:20.5f}" ) print( f"Rg = {Rg:20d}" ) # #### The format codes can be stored as variables # * If you have lots of print statements you only have to change ```w```, ```p```, and ```t``` below, instead of changing every print statement. # In[95]: w = 20 p = 5 t = 'e' print( f"Na = {Na:{w}.{p}{t}}" ) # ### Right, left, center # * ```<``` for left # * ```>``` for right # * ```^``` for center # * ```{var:20.4e} J/K" ) print( f"kB = {kB:^20.4e} J/K" ) # ### [Click here for more options](https://python-reference.readthedocs.io/en/latest/docs/functions/format.html)