print()¶
The function print() outputs character strings, whereby other Python data
types can easily be converted into strings and formatted, for example:
>>> import math
>>> pi = math.pi
>>> d = 28
>>> u = pi * d
>>> print(
... "Pi is",
... pi,
... "and the circumference with a diameter of",
... d,
... "inches is",
... u,
... "inches.",
... )
Pi is 3.141592653589793 and the circumference with a diameter of 28 inches is 87.96459430051421 inches.
F-Strings¶
F-strings can be used to shorten numbers that are too detailed for a text:
>>> print(f"The value of Pi is {pi:.3f}.")
The value of Pi is 3.142.
In {pi:.3f}, the format specification f is used to truncate the number
Pi to three decimal places.
In A/B test scenarios, you often want to display the percentage change in a key figure. F strings can be used to formulate them in an understandable way:
>>> metrics = 0.814172
>>> print(f"The AUC has increased to {metrics:=+7.2%}")
The AUC has increased to +81.42%
In this example, the variable metrics is formatted with = taking over
the contents of the variable after the +, displaying a total of seven
characters including the plus or minus sign, metrics and the percent sign.
.2 provides two decimal places, while the % symbol converts the decimal
value into a percentage. For example, 0.514172 is converted to +51.42%.
Values can also be converted into binary and hexadecimal values:
>>> block_size = 192
>>> print(f"Binary block size: {block_size:b}")
Binary block size: 11000000
>>> print(f"Hex block size: {block_size:x}")
Hex block size: c0
There are also formatting specifications that are ideally suited for CLI output, for example:
>>> data_types = [(7, "Data types", 19), (7.1, "Numbers", 19), (7.2, "Lists", 23)]
>>> for n, title, page in data_types:
... print(f"{n:.1f} {title:.<25} {page: >3}")
...
7.0 Data types............... 19
7.1 Numbers.................. 19
7.2 Lists.................... 23
In general, the format is as follows, whereby all information in square brackets is optional:
:[[FILL]ALIGN][SIGN][0b|0o|0x|d|n][0][WIDTH][GROUPING]["." PRECISION][TYPE]
The following table lists the fields for character string formatting and their meaning:
Field |
Meaning |
|---|---|
|
Character used to fill in |
|
Text alignment and fill character: <: left-aligned>: right-aligned^: centred=: Fill character after SIGN |
|
Display sign: +: Display sign for positive and negative
numbers-: Default value, - only for negative
numbers or space for positive |
|
Sign for integers: 0b: Binary numbers0o: Octal numbers0x: Hexadecimal numbersd: Default value, decimal integer with base 10n: uses the current locale setting to
insert the corresponding number separators |
|
fills with zeros |
|
Minimum field width |
|
Number separator: [1] ,: comma as thousands separator_: underscore for thousands separator |
|
For floating point numbers, the number of digits
after the point
For non-numeric values, the maximum length
|
|
Output format as number type or string … for integers: b: binary formatc: converts the integer to the corresponding
Unicode characterd: default value, decimal charactern: same as d, th the difference that it
uses the current locale setting to insert the
corresponding number separatorso: octal formatx: Hexadecimal format in base 16, using
lowercase letters for the digits above 9X: Hexadecimal format based on 16, using
capital letters for digits above 9… for floating point numbers: e: Exponent with e as separator between
coefficient and exponentE: Exponent with E as separator between
coefficient and exponentg: Standard value for floating point numbers,
whereby the exponent has a fixed width for large
and small numbersG: Like g, but changes to E if the
number becomes too large. The representations
of infinity and NaN are also written in capital
lettersn: Like g with the difference that it uses
the current locale setting to insert the
corresponding number separators%: Percentage. Multiplies the number by 100
and displays it in the fixed format f followed
by a percent sign |
Tip
A good source for F-strings is the help function:
>>> help()
help> FORMATTING
...
You can browse through the help here and find many examples.
You can exit the help function again with :–q and ⏎.
Debugging F-Strings¶
In Python 3.8, a specifier was introduced to help with debugging F-string
variables. By adding an equals sign =, the code is included within the
F-string:
>>> uid = "veit"
>>> print(f"My name is {uid.capitalize()=}")
My name is uid.capitalize()='Veit'
Formatting date and time formats and IP addresses¶
datetime supports the formatting of strings using the same syntax as
the strftime method for these objects.
>>> import datetime
>>> today = datetime.date.today()
>>> print(f"Today is {today:%d %B %Y}.")
Today is 26 November 2023.
The ipaddress module of Python also supports the formatting of
IPv4Address and IPv6Address objects.
Finally, third-party libraries can also add their own support for formatting
strings by adding a __format__ method to their objects.