[Tutor] way of dictating leading zeros

Andre Engels andreengels at gmail.com
Mon Mar 16 07:42:02 CET 2009


On Mon, Mar 16, 2009 at 3:47 AM, Patrick <optomatic at rogers.com> wrote:
> Hi Everyone
>
> I am trying to write a program that creates a bunch of svg files and
> then edits their values. I am then encoding them into a video. It's not
> encoding right because my filenames are wrong. They have to have a
> sequence. Right now they are 1.svg, 2.svg, 3.svg etc but they should be
> 001.svg, 002.svg, 003.svg. At the moment I think 12.svg is ending up
> before 2.svg because it starts with 1.
>
> Is there an easy way to dictate how many digits a number will occupy
> that also allows for leading zeros?

"%03i"%i

for example:

"%03i"%2 equals "002" and "%03i"%12 equals "012". Of course in your
case you can combine the adding of .svg at once:

"%03i.svg"%2 equals "002.svg".

Explanation of what this means:

"blabla %s bla"%something

means that the '%s' is to replaced by the string representation of
something. Changing %s to %i means that the something is read as an
integer for that, and %03i means that the integer has to be shown
padded with zeroes with a length of (minimal) 3.


-- 
André Engels, andreengels at gmail.com


More information about the Tutor mailing list