[Tutor] more fun and games with padded numbers

John Fouhy john at fouhy.net
Fri Jan 12 01:17:14 CET 2007


On 12/01/07, Christopher Spears <cspears2002 at yahoo.com> wrote:
> Let's say I have a series of files that are named like
> so:
>
> 0001.ext
> 0230.ext
> 0041.ext
> 0050.ext
>
> How would I convert these from a padding of 4 to a
> padding of 1?  In other words, I want the files to be
> renamed as:
>
> 1.ext
> 230.ext
> 41.ext
> 50.ext
>
> 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']

-- 
John.


More information about the Tutor mailing list