[Tutor] Asterisk

Gregor Lingl glingl@aon.at
Wed Dec 4 17:36:01 2002


Adam Vardy schrieb:

>Why does this happen? It is a strange expression.
>
>  
>
>>>>'%*.*f' % (6,3,1.41421356)
>>>>        
>>>>
>' 1.414'
>
>  
>
Strange? Maybe! But first of all: useful:

 >>> "%*.*f" % (6,3,1.41421356)
' 1.414'
 >>> "%*.*f" % (10,5,1.41421356)
'   1.41421'
 >>> "%*.*f" % (10,7,1.41421356)
' 1.4142136'
 >>> "%*.*f" % (10,1,1.41421356)
'       1.4'
 >>> len("%*.*f" % (10,1,1.41421356))
10
 >>> len("%*.*f" % (6,3,1.41421356))
6
 >>>

It allows to use computed (e.g. floating-point) formats
instead of predetermined ones.

 >>> "%*.*f" % (6,3,1.41421356)
for instance uses "%6.3f" for 1.41421356
 >>> a = 7
 >>> b = 2
 >>> "%*.*f" % (a,b,1.41421356)
'   1.41'
 >>>
HTH, Gregor