-5

How can I generate 14 digit serial numbers in python where the last for 4 digit will be 0001 the next will be 0002 ...... 0011 and so on? This is how I want the format of the number to be 12101010010001 below is the breakdown of the format.

First three digits (121) = Local Govt. ID
4th & 5th digits (01) = Zonal ID
6th & 7th digits (01) = Area ID
8th to 10th digits (001) = CDA No.
Last four digits (0001) Property No.
Chandan
  • 571
  • 4
  • 21
MyCDAR
  • 55
  • 7
  • You format is somewhat underspecified. What are the constraints on such a number compared to an arbitrary 14-digit number? – phipsgabler Apr 09 '20 at 10:10
  • what have you tried so far? `["{}{}{}{}{:04d}".format(LGI, ZID, AID, CDA, x) for x in range(1, 10000)]` as a general case, or just `["1210101001{:04d}".format(x) for x in range(1, 10000)]` – Andrew Apr 09 '20 at 10:12
  • @phipsgabler I want the constraints of the number to be 14 digit long – MyCDAR Apr 09 '20 at 10:21
  • @Andrew thanks for your response this will be as a general case like the example you gave` ["{}{}{}{}{:04d}".format(LGI, ZID, AID, CDA, x) for x in range(1, 10000)]` I am trying this out now – MyCDAR Apr 09 '20 at 10:25
  • tom-karzes answer is excellent for the general case – Andrew Apr 09 '20 at 10:28

1 Answers1

3

I would construct it as a string:

sn = "{:03}{:02}{:02}{:03}{:04}".format(121, 1, 1, 1, 1)

This gives sn the value '12101010010001', zero-padding the fields to the desired width. If you want to convert the result to an integer (as opposed to leaving it as a string), just use int(sn).

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41