My First Python Script

James Stroud jstroud at mbi.ucla.edu
Thu Sep 15 23:48:04 EDT 2005


Try the % operator with strings.

For instance: "%03.d" % 5

py> "%03.d" % 5
'005'

This operator, from what I have experienced, has the same properties as the 
unix printf. In python, I think its called "string formatting".

James

On Thursday 15 September 2005 20:33, Ed Hotchkiss wrote:
> So I have a script, first part of a script that I am making to determine
> each possible combination of IP addresses that are out there.
>  This part is just to get the end of the IP (the last 3 digits). So, they
> are from 0 - 255. I just want to print 0 - 255, HOWEVER
>  If the number is only say ... 3, then I want mySet to be 003, not 3. I
> need to add the zeros to the front.
> My code is below - I guess that there is something to do with my variable
> types etc. I am not used to the way that Python handles variable types so I
> am kind of lost, can anyone help ...
>
> # Script to Evaluate every possible IP Address Combo
>
> # 9/15/05
>
> def ZeroThrough255():
>
> x = 0
>
> while x <= 255:
>
> if len(x) == 1:
>
> mySet = '00' + str(x)
>
> elif len(x) == 2:
>
> mySet = '0' + str(x)
>
> else:
>
> mySet = x
>
> print mySet
>
> x +=1
>
> ZeroThrough255()
>
> --------------------
> The file is also attached.

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list