best way to remove leading zeros from a tuple like string

brucegoodstein at gmail.com brucegoodstein at gmail.com
Mon May 21 16:16:22 EDT 2018


On Monday, May 21, 2018 at 1:05:52 PM UTC-4, Rodrigo Bistolfi wrote:
> >>> 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
> >


I am not familiar with the repr statement.
Looking into your response, I like it.
There is no need to add-in the parentheses.

With the repr statment:
>>> bb = repr (tuple (int (i) for i in s [1: -1] .split (',')))
>>> bb
'(128, 20, 8, 255, -1203, 1, 0, -123)'
>>> type(bb)
<class 'str'>
>>>

Without the repr statement:
>>> aa = (tuple (int (i) for i in s [1: -1] .split (',')))
>>> aa
(128, 20, 8, 255, -1203, 1, 0, -123)
>>> type(aa)
<class 'tuple'>
>>>

Thanks,
Bruce






More information about the Python-list mailing list