Skip to content

Output of variables

The usual way of outputting the contents of a variable is the print-function, which outputs a string on the command line or in Jupyter notebooks. The use is very simple:

Example
count = 11
print (count) # outputs "11"

With decimal numbers, the significant digits are output without terminating zeros:

Example
share = 1/9
print(share) # 0.1111111111111111

share = 0.1
print(share) # 0.1

Exceptions are very large or very small numbers where scientific notation is used:

Example
print(10.**100) # 1e+100

With integers, the whole number is always specified, as this is stored with any number of significant digits:

Example
print(10**100) # 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Character strings and truth expressions are unsurprisingly reproduced in full:

Example
print (True) # True
print ("Hallo") # Hallo

With containers, the representation of the containers is obtained as it would also be assumed as programme code input, whereby the individual elements (both value data types or container data types) are in turn represented as a single element of the respective data type:

Example
print ([]) # []
print (["A", 2, "B"]) # ['A', 2, 'B']
print (set([1,1,2])) # {1, 2}
print ([["A", "B"], [1, 2]]) # [['A', 'B'], [1, 2]]

You can pass any number of (positional) arguments to the print function. These are output separated by spaces:

Example
print("A", 2) # A 2

f-strings

As you often want to output not only the content of an expression, but typically embed the output in other text, it is often helpful to have more influence on the display. One way to do this is to use f-strings, i.e. character strings that are prefixed with f before the inverted commas.

Example
kinetic_energy = 42.4711
print (f"Kinetic Energy: {kinetic_energy} eV") # Kinetic Energy: 42.4711 eV

The placeholder {kinetic_energy} was evaluated and replaced by the content (converted to a character string). Formally, this is equivalent to

Example
kinetic_energy = 42.4711
print ("Kinetic Energy:", kinetic_energy, "eV") # Kinetic Energy: 42.4711 eV

However, the placeholder can also contain arithmetic operations:

Example
kinetic_energy = 42.4711
hartree = 27.2113862459
print (f"Kinetic Energy: {kinetic_energy/hartree} a.u.") # Kinetic Energy: 1.560784137059508 a.u.

If the last character of the placeholder is an equals sign =, the name or expression and the value are output together:

Example
kinetic_energy = 42.4711
hartree = 27.2113862459
print (f"{kinetic_energy=} {kinetic_energy/hartree=}") # kinetic_energy=42.4711 kinetic_energy/hartree=1.560784137059508

In addition to this syntactic simplification, f-strings allow more influence on the concrete representation, which often improves the readability of the output. For this purpose, a second optional section containing format instructions is inserted in the placeholder {expression:format} with the colon :.

A fixed width can initially be defined for the output:

Example
firstname = "Alessandro"
lastname = "Binomi"
print(f"| {firstname:20} | {lastname:20} |")
firstname = "Julius"
lastname = "Eigen"
print(f"| {firstname:20} | {lastname:20} |")
# | Alessandro           | Binomi               |
# | Julius               | Eigen                |

Now the output can be aligned (note the greater than symbol > and caret ^)

Example
firstname = "Alessandro"
lastname = "Binomi"
print(f"| {firstname:>20} | {lastname:^20} |")
firstname = "Julius"
lastname = "Eigen"
print(f"| {firstname:>20} | {lastname:^20} |")
# |           Alessandro |        Binomi        |
# |               Julius |        Eigen         |

For integers and decimals, the display is automatically right-aligned if more space is given:

Example
kinetic_energy = 42.4711
print(f"|{kinetic_energy:10}|") # |   42.4711|

The number of decimal places can be specified after the width of the field, separated by a dot. In this case, however, it must be made clear whether the scientific notation (suffix e) or decimal notation (suffix f) is required:

Example
kinetic_energy = 42.4711
print(f"|{kinetic_energy:10.2e}|") # |  4.24e+01|
print(f"|{kinetic_energy:10.2f}|") # |     42.47|