How to retain leading zeros of int variables?

前端 未结 3 835
情书的邮戳
情书的邮戳 2020-12-03 18:09

Below is a section of code which is part of a functional decryption and encryption program.

while checkvar < maxvar: # is set to < as maxvar is 1 to hig         


        
3条回答
  •  温柔的废话
    2020-12-03 18:46

    Though the comments above are true regarding 1, 01, and 001, are all the same as an int, it can be very helpful in temporal modeling, or sequential movie making to maintain the leading zeros. I do it often to ensure movie clips are in proper order. The easy way to do that is using zfill() to ensure the str version of the number has at least the number of characters you tell it, and does so by filling in the left-side of the string "number" with zeros.

    >>> x = int(1)    
    >>> NewStringVariable = str(x).zfill(3)    
    >>> print NewStringVariable    
    001    
    >>> NewStringVariable = str(x).zfill(5)    
    >>> print NewStringVariable    
    00001
    

提交回复
热议问题