00
is the same number as 0
.
In [59]: 00 == 0
Out[59]: True
But Python will always represent this number as 0
, never 00
. If you want to preserve 00
then you must use strings:
In [56]: a = '2,00,02'
In [57]: a.split(',')
Out[57]: ['2', '00', '02']
If you want to convert a
to a list of ints
while also preserving the leading zeros, then you'll need to keep track of two lists:
In [65]: astr = a.split(',')
In [66]: astr
Out[66]: ['2', '00', '02']
In [67]: aint = map(int, astr)
In [68]: aint
Out[68]: [2, 0, 2]
In [69]: zip(astr, aint)
Out[69]: [('2', 2), ('00', 0), ('02', 2)]
Use aint
for numeric computations, and astr
for string formatting/output.
Note that leading 0's in Python are used to represent octal numbers:
In [62]: 011
Out[62]: 9
since 11 base 8 is 9.
So if the string was a = '2,011,02'
, then you'll need to clarify if you want the integer values [2,11,2]
or [2,011,02]
which is equal to [2,9,2]
.
In [70]: [2, 011, 02] == [2, 9, 2]
Out[70]: True
And if your string was a = '2,09,02'
, then if you convert each numeric string to an int you'd get [2, 9, 2]
, but if you want [2, 09, 02]
then you'd get a SyntaxError
since 09
is not a valid octal number.