Thursday, July 23, 2009


Python Width and Precision:

A conversion specifier may include a field width and a precision. 
The width is the minimum number of characters reserved for a formatted value. The precision is (for a numeric conver-sion) the number of decimals that will be included in the result or (for a string conversion) the maximum number of characters the formatted value may have.

These two parameters are supplied as two integer numbers (width first, then precision), 
separated by a . (dot). Both are optional, but if you want to supply only the precision, 
you must also include the dot:

>>> '%10f' % pi      # Field width 10
'  3.141593'

>>> '%10.2f' % pi    # Field width 10, precision 2
'      3.14'
>>> '%.2f' % pi      # Precision 2
'3.14'
>>> '%.5s' % 'Guido van Rossum'
'Guido'

You can use an * (asterisk) as the width or precision (or both). 
In that case, the number will be read from the tuple argument:

>>> '%.*s' % (5, 'Guido van Rossum')
'Guido'

No comments:

Data Science - 103 (Kapil Sharma)

Cloud Computing Basic:-  In this the PC is on service provider data center and secuirty maintenance and upgrades are done by the service pro...