Formatting numbers, precision, output¶

TLDR¶

Define some constants¶

In :
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 :
print(    f"Na  = {Na:20.7e} #/kmol" )
print(    f"atm = {atm:20.3f} kPa"   )
print(    f"Rg  = {Rg:20d} J/kmol*K" )
Na  =        6.0221409e+26 #/kmol
atm =              101.325 kPa
Rg  =                 8314 J/kmol*K

Details¶

There are three main formatting methods.

• % formatting
• str.format()
• f-strings

Here, we'll use the newer f-string method.¶

• f-string introduction with a brief summary of the other methods.
• Overview of the three methods.
• The str.format() and f-string methods have a lot of similiarities.
• This link gives a good description of the str.format() method which is good for understanding.
• This site 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 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 :
print(  "The value of pi is", pi   )
The value of pi is 3.141592653589793

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 :
print(  f"The value of pi is {pi}"   )
The value of pi is 3.141592653589793
In :
print(  f"kB={kB} and Na={Na}"   )
kB=1.3806485279e-23 and Na=6.0221409e+26
In :
p = f"k={kB} and Na={Na}"
print(p)
k=1.3806485279e-23 and Na=6.0221409e+26

Generic python expressions can go inside { }¶

In :
print(   f"pi^2 = {pi**2}"   )
pi^2 = 9.869604401089358

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 :
print(    f"Rg = {Rg:d}"   )
print(    f"Rg = {Rg:f}"   )
print(    f"Rg = {Rg:e}"   )
Rg = 8314
Rg = 8314.000000
Rg = 8.314000e+03

Precision¶

• Format as {var:.5t}, where the .5 means 5 decimal places.
In :
print(    f"pi = {pi:.5f}"    )
print(    f"pi = {pi:.5e}"    )
pi = 3.14159
pi = 3.14159e+00

Width¶

• Specify a fixed width of a number.
• {var:w.pt} where w is the full width and p is the precision.
In :
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}"      )
pi  =              3.14159
Na  =          6.02214e+26
kB  =          1.38065e-23
atm =            101.32500
Rg  =                 8314

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 :
w = 20
p = 5
t = 'e'
print(    f"Na  = {Na:{w}.{p}{t}}"    )
Na  =          6.02214e+26

Right, left, center¶

• < for left
• > for right
• ^ for center
• {var:<w.pt}
In :
print(    f"kB = {kB:<20.4e} J/K"    )
print(    f"kB = {kB:>20.4e} J/K"    )
print(    f"kB = {kB:^20.4e} J/K"    )
kB = 1.3806e-23           J/K
kB =           1.3806e-23 J/K
kB =      1.3806e-23      J/K