May 2019
Beginner
528 pages
29h 51m
English
Proper text formatting makes data easier to read and understand. Here, we present many text-formatting capabilities.
You’ve seen basic string formatting with f-strings. When you specify a placeholder for a value in an f-string, Python assumes the value should be displayed as a string unless you specify another type. In some cases, the type is required. For example, let’s format the float value 17.489 rounded to the hundredths position:
In [1]: f'{17.489:.2f}'Out[1]: '17.49'
Python supports precision only for floating-point and Decimal values. Formatting is type dependent—if you try to use .2f to format a string like 'hello', a ValueError occurs. So the presentation type f in the format specifier ...
Read now
Unlock full access