best way to remove leading zeros from a tuple like string

Peter Otten __peter__ at web.de
Mon May 21 03:26:51 EDT 2018


bruceg113355 at gmail.com wrote:

> Looking over the responses, I modified my original code as follows:
> 
>>>> s = "(0000128, 020, 008, 255, -1203,01,-000, -0123)"
>>>> ",".join([str(int(i)) for i in s[1:-1].split(",")])
> '128,20,8,255,-1203,1,0,-123'

I think this looks better with a generator instead of the listcomp:

>>> ",".join(str(int(i)) for i in s[1:-1].split(","))
'128,20,8,255,-1203,1,0,-123'





More information about the Python-list mailing list