14

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 high for the index of var
    #output.append("%02d" % number)
    i =ord(var[checkvar]) - 64 # Gets postional value of i
    i = ("%02d" % i)
    if (checkvar + 1) < maxvar:
        j =ord(var[(checkvar + 1)]) - 64 # Gets postional value of i
        j = ("%02d" % j)
        i = str(i) + str(j) #'Adds' the string i and j to create a new i
    li.append(int(i))
    checkvar = checkvar + 2

print li

As you can see the two variables i and j are first treated as string to add a 0 in front of any single digit numbers (as string). These variables then are combined to make a four digit number (still as a string). Later in the program the number created are used in a pow() function, as ints remove any leading zeros.

My question: Is it possible to force python to keep the leading zero for ints? I have and continued to search online.

Edit

To help people I have included the encryption part of the program. This is where the problem lies. The variables created in the above code are passed through a pow() function. As this can't handle strings I have to convert the variables to ints where the leading zero is lost.

#a = li[]
b=int(17)#pulic = e
c=int(2773)#=n

lenli=int(len(li))
enchecker = int(0)

#encrpted list
enlist = []

while enchecker < lenli:
    en = pow(li[enchecker],b,c)#encrpyt the message can only handle int
    enlist.append(int(en))
    enchecker = enchecker + 1

print enlist
martineau
  • 119,623
  • 25
  • 170
  • 301
Dan1676
  • 1,685
  • 8
  • 24
  • 35
  • 1
    You have `i = "%02d" %i`, which is the right thing to do here… why does that not work for you? – inspectorG4dget Nov 21 '12 at 17:46
  • 2
    No, integer values are not stored with leading zero. Else how would you define how many leading zeros to consider? You need to format it to print it in string form, which you are using it currently. – Rohit Jain Nov 21 '12 at 17:46
  • When Python stores an integer, under the covers it's just a bunch of ones and zeroes. The decimal number you see is how that bunch of bits gets converted into a human readable representation. You can use string formatting functions (like the one you're using) to add leading zeroes to this representation, but you can't store leading zeroes in the int itself. It just doesn't make sense. – NullUserException Nov 21 '12 at 17:49
  • @inspectorG4dget. That code does work. the problem is int variable remove the 0. At Rohit Jain. If you are asking how many zeros I would consider that that is already done (while it is still a string). If you are talking about python defining the zeros then I don't know – Dan1676 Nov 21 '12 at 17:51

4 Answers4

35

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
martineau
  • 119,623
  • 25
  • 170
  • 301
user9297
  • 469
  • 1
  • 4
  • 2
  • 4
    This answer should have been accepted, in my opinion. – FaCoffee Jan 23 '17 at 11:48
  • 3
    This makes a "string" and not an "integer" type. "TYPE" is important in the context. It's a good approach if we want the new variable to be of "string type", but if we need "integer" type, then no. – Xonshiz Dec 26 '17 at 09:59
  • best answer. But what if the number of leading zeroes is variable, as in we don't know but we want to preserve input number of leading zeroes – deadcode Apr 27 '18 at 10:25
  • 2
    @Xonshiz maintaining leading 0s in an int type is meaningless. 1, 01, and 001 are all just 1, the only time you should ever need leading zeros is for display purposes. – ThinkingInBits Mar 15 '20 at 14:14
  • Well, that makes sense. However, I just pointed it out, in case somebody would come looking for this and then scratch their heads why it's throwing some other error. – Xonshiz Mar 16 '20 at 14:14
15

The concept of leading zeros is a display concept, not a numerical one. You can put an infinite number of leading zeros on a number without changing its value. Since it's not a numeric concept, it's not stored with the number.

You have to decide how many zeros you want when you convert the number to a string. You could keep that number separately if you want.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

I was getting date strings in the format of hhmmss coming from the serial line of my Arduino.

Suppose I got s = "122041"; this would be 12:20:41, however 9am would be 090000.

The statement print "%d" % (s) provokes a run time error because the 9 is not an octal number and is hence an illegal character.

To fix this problem:

print "%06d" % (int(s))
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
1

Try this:

number = 1
print("%02d" % (number,))

or:

print("{:02d}".format(number))

The explanation of "%02d":

% - This tells the interpreter that a variable should be inserted here.

02 - This tells the interpreter to expect the variable to be 2 in length.

d - This tells the interpreter to expect a number, or should we say a"'d’igit".

Gavriel Cohen
  • 4,355
  • 34
  • 39