[Tutor] more fun and games with padded numbers

Kent Johnson kent37 at tds.net
Fri Jan 12 12:00:58 CET 2007


John Fouhy wrote:
> On 12/01/07, Christopher Spears <cspears2002 at yahoo.com> wrote:
>> At first I used strip(), which was a mistake because
>> it removed all the zeroes.
> 
> Are you sure?  If you used .replace('0', '') it would remove all the
> zeros.  But this is what strip is for.
> 
>>>> lst = ['0001.ext', '0230.ext', '0041.ext', '0050.ext']
>>>> [s.lstrip('0') for s in lst]
> ['1.ext', '230.ext', '41.ext', '50.ext']

I just want to highlight that your code uses lstrip(), not strip(). 
lstrip() is perfect for this problem, it removes leading characters 
only, in this case leading 0s.

Kent



More information about the Tutor mailing list