pi = 3.14159265358979323846264338
Na = 6.0221409e+26 # #/kmol
atm = 101.325 # kPa
kB = 1.3806485279E-23 # J/K
Rg = 8314 # J/kmol*K
f"some text, value of var inserted here: {var}"
{var}
inserts the value of variable var
{var}
in a single string.{var:w.pt}
:
w
is the width the variable should take (optional).p
is the precision (optional)t
is the type (optional)e
for scientific 6.02E23f
for floating point number 3.14d
for integer 101s
for stringprint( 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
There are three main formatting methods.
print( "The value of pi is", pi )
The value of pi is 3.141592653589793
f"My string"
f"My string {varName}"
{varName}
is replaced with the value of the variable.print( f"The value of pi is {pi}" )
The value of pi is 3.141592653589793
print( f"kB={kB} and Na={Na}" )
kB=1.3806485279e-23 and Na=6.0221409e+26
p = f"k={kB} and Na={Na}"
print(p)
k=1.3806485279e-23 and Na=6.0221409e+26
{ }
¶print( f"pi^2 = {pi**2}" )
pi^2 = 9.869604401089358
{var:t}
where t
is one ofe
or E
for scientific, like 6.02E23f
for floating point, like 3.14d
for integers, like 101 (d is for decimal number, as opposed to, say, binary)g
for general number (python chooses),s
for string).print( f"Rg = {Rg:d}" )
print( f"Rg = {Rg:f}" )
print( f"Rg = {Rg:e}" )
Rg = 8314 Rg = 8314.000000 Rg = 8.314000e+03
{var:.5t}
, where the .5
means 5 decimal places.print( f"pi = {pi:.5f}" )
print( f"pi = {pi:.5e}" )
pi = 3.14159 pi = 3.14159e+00
{var:w.pt}
where w
is the full width and p
is the precision.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
w
, p
, and t
below, instead of changing every print statement.w = 20
p = 5
t = 'e'
print( f"Na = {Na:{w}.{p}{t}}" )
Na = 6.02214e+26
<
for left>
for right^
for center{var:<w.pt}
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