how to improve this simple block of code

Juho Schultz juho.schultz at helsinki.fi
Wed Jan 11 09:12:58 EST 2006


py wrote:
> Say I have...
> x = "132.00"
> 
> but I'd like to display it to be "132" ...dropping the trailing
> zeros...I currently try this
> 
> if x.endswith("0"):
>     x = x[:len(x)-1]
> if x.endswith("0"):
>     x = x[:len(x)-1]
> if x.endswith("."):
>     x = x[:len(x)-1]
> 
> I do it like this because if
> x = "132.15"  ...i dont want to modify it.  But if
> x = "132.60" ...I want it to become "132.6"
> 
> is there a better way to do this?  It seems a bit ugly to me.
> 
> T.I.A
> (thanks in advance)
> 

x = x.rstrip('0') # removes trailing zeros
x = x.rstrip('.') # removes trailing dot(s)



More information about the Python-list mailing list