best way to remove leading zeros from a tuple like string

Rodrigo Bistolfi rbistolfi at gmail.com
Mon May 21 13:05:33 EDT 2018


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

2018-05-21 4:26 GMT-03:00 Peter Otten <__peter__ at web.de>:

> 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'
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list