Newbie question - leading zeros

wittempj@hotmail.com martin.witte at gmail.com
Fri Oct 13 16:24:03 EDT 2006


Rainy wrote:
> eldorado wrote:
> > I have looked around and cannot seem to find a way to strip leading zeros
> > off of values in a dictionary. Basically, I am looking to do a for loop
> > and any value that has one or more leading zeros would be stripped. Any
> > pointers would be appreciated. Thanks
> >
> > --
> > Randomly generated signature
> > ICMP: The protocol that goes PING!
>
> import string
> >>> string.lstrip('0001', '0')
> '1'
>
> Hope this willhelp
>
>  -Rainy

string.lstrip is deprecated, so when OPs dictionary has as values
strings it's better to do something like

x = {'a key':'0001', 'another key': '0002'}
for k, v in x.iteritems():
    x[k] = v.lstrip('0')




More information about the Python-list mailing list