Simple string formatting question
Fredrik Lundh
fredrik at pythonware.com
Thu Apr 6 08:32:46 EDT 2006
Steven D'Aprano wrote:
> Here is a (quick and dirty) reference implementation:
>
> def format(f, width=3):
> fs = '%%.%df' % width
> s = fs % f
> return s.rstrip('0').rstrip('.')
>
> Is there a way of getting the same result with just a
> single string format expression?
not with % itself, but you can do it all in one line:
def format(f, width=3): return ("%.*f" % (width, f)).rstrip(".0")
</F>
More information about the Python-list
mailing list