Mega Code Archive

 
Categories / Python Tutorial / String
 

Width and Precision

from math  import pi  print '%10f' % pi # Field width 10  print '%10.2f' % pi # Field width 10, precision 2  print '%.2f' % pi # Precision 2  print '%.5s' % 'Guido van Rossum'  use an * as the width or precision (or both), in which case the number will be read from the tuple argument print '%.*s' % (5, 'Guido van Rossum')