Newbie question - leading zeros

Gary Herron gherron at islandtraining.com
Fri Oct 13 14:28:08 EDT 2006


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
>
>   
There is nowhere enough information in your question to answer it well,
but I'll make some guesses.

If your values are strings that have leading zeros, then lstrip may be
what you want.
>>> str = "00xyz"
>>> str.lstrip('0')
'xyz'

If your values are integers -- then the question does not make sense. 
The internal representation of a number (binary zeros and ones) is not
something you can change.

If your values are integers and something is printing them out with
leading zeros -- then change the code that is doing the formatting for
that print.  For example, here are several ways to convert an int to a
printable string with and without leading zeros:
>>> i = 123
>>> '%d' % i
'123'
>>> '%5d' % i
'  123'
>>> '%05d' % i
'00123'

Hope that helps,
Gary Herron




More information about the Python-list mailing list